一、下載並安裝
下載: wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz
解壓:tar –vzxf elasticsearch-5.2.2.tar.gz
需要配置一些東西: vi /etc/security/limits.conf
添加一些內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
vi /etc/security/limits.d/90-nproc.conf
修改一下內容
修改如下內容: * soft nproc 1024
#修改爲 : * soft nproc 2048
vi /etc/sysctl.conf
添加如下配置
vm.max_map_count=655360
並執行命令:
sysctl –p
由於安全問題elasticsearch不允許root用戶登錄,所以增加個用戶單獨去啓動它
groupadd tend
useradd tend -g tend -p elasticsearch-5.2.2
chown -R tend:tend elasticsearch
su tend
cd elasticsearch-5.2.2
啓動elasticsearch
./bin/elasticsearch
ElasticSearch插件head 安裝
下載:wget https://github.com/mobz/elasticsearch-head/archive/master.zip
解壓:unzip master.zip
一、Head需要node的支持,所以在此需要安裝node(省略安裝步驟)
二、安裝grunt 在head的目錄下執行npm install -g grunt-cli
執行 npm install -g grunt
如果安裝過程比較慢,建議臨時使用taobao鏡像(我就是用鏡像安裝的)
例如安裝grunt-cli命令如下:
npm install -g grunt-cli--registry=https://registry.npm.taobao.org
安裝完成後查看版本grunt –version
增加hostname屬性 設置爲”0.0.0.0”
下載測試數據:
wget https://github.com/bly2k/files/raw/master/accounts.zip
上傳到elasticsearch
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty'--data-binary @accounts.json
檢查集羣健康與否:
curl 'localhost:9200/_cat/health?v'
獲得集羣中的節點列表
curl 'localhost:9200/_cat/nodes?v'
列出所有的索引
curl'localhost:9200/_cat/indices?v'
創建一個索引
curl -XPUT 'localhost:9200/customer?pretty'
將一個簡單的客戶文檔索引到customer索引、“external”類型中,這個文檔的ID是1
curl -XPUT'localhost:9200/customer/external/1?pretty' -d '
{ "name": "John Doe" }'
取出索引的文檔
curl -XGET'localhost:9200/customer/external/1?pretty'
刪除索引
curl -XDELETE 'localhost:9200/customer?pretty'
使用請求體方法(一旦你取回了搜索結果,elasticsearch就完成了使命它不會維護任何服務器端的資源或者在你的結果中打開遊標)
curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all":{} }
}'
- took ——Elasticsearch執行這個搜索的耗時,以毫秒爲單位
- timed_out —— 指明這個搜索是否超時
- _shards —指出多少個分片被搜索了,同時也指出了成功/失敗的被搜索的shards的數量
hits —— 搜索結果
- hits.total —— 能夠匹配我們查詢標準的文檔的總數目
- hits.hits —— 真正的搜索結果數據(默認只顯示前10個文檔)
-hists._score 相關度,值越大相關度越高
下面做了一次match_all並只返回第一個文檔:
curl -XPOST 'localhost:9200/bank/_search?pretty'-d '
{
"query": { "match_all": {} },
"size": 1
}'
注:如果沒有指定size則默認爲10
curl-XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
如果from不指定默認爲0
下面這個例子返回賬戶編號爲20的文檔:
curl-XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20} }
}'
下面這個例子返回地址中包含“mill”的所有賬戶:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {"match": { "address": "mill" } }
}'
或者"query": { "match": {"address": "mill lane" } } 查詢包含mill或者lane的賬戶
"query": {"match_phrase": { "address": "mill lane" } } 查詢包含 mill跟lane的賬戶,它會去匹配短語“mill lane”;
現在這個例子組合了兩個match查詢,這個組合查詢返回包含“mill”和“lane”的所有的賬戶:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": {"address": "mill" } },
{ "match": {"address": "lane" } }
]
}
}
}'
其中 must是必須包含(&&),should是或(||)must_not是都不(not no)
balance在2000~3000(閉區間)的賬戶
curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"filtered": {
"query": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
以上這種方式在2.2版本被棄用。改用bool的方式
curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{"query":{
"bool":{
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}_bulk 多條數據一起查詢
POST /my_index/posts/_bulk
{
"index": {
"_id":
"1"}}
{
"tags": [
"search"] }
{
"index": {
"_id":
"2"}}
{
"tags": [
"search",
"open_source"] }
{
"index": {
"_id":
"3"}}
{
"other_field":
"some data"}
{
"index": {
"_id":
"4"}}
{
"tags":
null}
{
"index": {
"_id":
"5"}}
{
"tags": [
"search",
null] }
幾種過濾器
範圍過濾器(range)
{"query":{
"bool":{
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
exits過濾器
過濾掉給定字段沒有值的文檔
{
"post_filter":{
"exists":{
"field":"year"
}
}
}
missing過濾器
過濾掉給定字段有值或缺失的文檔
{
"post_filter":{
"missing":{
"field":"year",
"null_value":0,
"existence":true
}
}
}
腳本過濾器
過濾掉髮表在一個世紀以前的書
{
"post_filter":{
"script":{
"script":"now - doc['year'].value > 100",
"params":{"now":2012}
}
}
}
類型過濾器
當查詢運行在多個索引上時,有用
{
"post_filter":{
"type":{
"value":"book"
}
}
}
限定過濾器 限定每個分片返回的文檔數
{
"post_filter":{
"limit":{
"value":1
}
}
}
標識過濾器
指定標識符爲1,2,3的文檔
{
"post_filter":{
"ids":{
"type":["book"],
"values":[1,2,3]
}
}
}
組合過濾器
{
"query": {
"bool": {
"must": {
"range": {
"year": {
"gte": 1930,
"lte": 1990
}
}
},
"should": {
"term": {
"available": true
}
},
"boost": 1
}
}
}
聚合查詢
按年齡降序
curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{
"aggs": {
"all_age": {
"terms": {
"field": "age"
}
}
}
}'