ES索引操作


1、創建測試索引

PUT /test_index
{
"mappings": {
"test_type":{
"properties": {
"code":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik"
},
"count":{
"type": "integer"
}
}
}
},
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1,
"max_result_window":10000,
"analysis": {
"analyzer": {
"ik":{
"tokenizer":"ik_max_word",
"stopwords":["的","是"]
}
}
}
}
}

2、插入數據

PUT test_index/test_type/1  #4代表索引記錄的唯一標識,類似數據庫表中主鍵
{
"code":"001",
"name":"萬科企業股份有限公司"
}

PUT test_index/test_type/2
{
"code":"002",
"name":"萬達集團股份有限公司"
}

PUT test_index/test_type/3
{
"code":"003",
"name":"阿里巴巴(中國)有限公司"
}

PUT test_index/test_type/4
{
"code":"004",
"name":"中國平安"
}

3、刪除數據

DELETE test_index/test_type/4  #4代表索引記錄的唯一標識,類似數據庫表中主鍵

4、更新數據

回復刪除的4數據

PUT test_index/test_type/4
{
"code":"004",
"name":"中國平安"
}

①覆蓋更新

PUT test_index/test_type/4
{
"name":"中國平安"
}

②更新字段值

POST test_index/test_type/4/_update
{
"doc": {
"name":"中國平安保險(集團)股份有限公司"
}

5、查詢數據

在kibana中默認只顯示10條記錄

①查詢所有

方式1:

GET test_index/test_type/_search

方式2:

GET test_index/test_type/_search
{
"query":{
"match_all": {}
}
}

②根據指定分詞字段查詢

name:為索引中的中文分詞字段

GET test_index/test_type/_search
{
"query":{
"match": {
"name": "中國"
}
}
}

③、根據指定不分詞字段查詢

GET test_index/test_type/_search
{
"query":{
"term": {
"code": "004"
}
}
}

④、根據文檔id查詢

查詢描述:pXrY0GsBN9ZpEwHZ14it:文檔id

方式1:GET 索引/類型/文檔id

GET test_index/test_type/4

方式2:_id文檔唯一的id

GET test_index/test_type/_search
{
"query":{
"match": {
"_id": "4"
}
}
}

 ⑤多條件查詢

多條件and關系:

#邏輯:and關系
GET test_index/test_type/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": { "question": "淘寶"}},
        {"match": { "question": "軟件"}}
      ]
    }
  }
}

 

GET test_index/test_type/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name":"中國平安保險(集團)股份有限公司"
}
},
{
"term": {
"code": "004"
}
}
]
}
}
}

多條件or關系:

GET test_index/test_type/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"name":"中國平安保險(集團)股份有限公司"
}
},
{
"term": {
"code": "004"
}
}
]
}
}
}

多條件and和or關系混合使用:

GET test_index/test_type/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "name":"中國平安保險(集團)股份有限公司"
          }
        },
        {
          "term": {
            "code": "004"
          }
        }
      ],
      "must": [
        {
          "match": {
            "count": 2
          }
        }
      ]
    }
  }
}

自定義排序:

GET test_index/test_type/_search
{
  "query": {
    "bool": {
      "must" : [
        {
          "match" : {
            "name" : {
              "query" : "中國平安"
            }
          }
        }
      ],
    "adjust_pure_negative": true,
    "boost": 1.0
    }
  },
  "_source": {
    "includes": ["code", "name"],
    "excludes": []
  },
  "sort": [{
    "count": {
      "order": "desc"
    }
  }]
}

 

⑥指定查詢條數 

GET test_index/test_type/_search
{
  "query": {
    "match": {
      "question": "淘寶"
    }
  },
  "from": 1,  #指定位移
  "size": 5 #指定查詢條數
}

⑦指定查詢返回的字段

GET test_index/test_type/_search
{
  "_source": ["question","nlp"],  #返回字段數組
  "query": {
    "match": {
      "question": "淘寶"
    }
  }
}

⑧控制加載的字段

GET test_index/test_type/_search
{
  "_source": {"includes": ["question","nlp"],"excludes": ["isSatisfied"]}, 
  "query": {
    "match": {
      "question": "淘寶"
    }
  }
}

⑨通配符

GET test_index/test_type/_search
{
  "_source": {"includes": ["quest*"],"excludes": ["*Date"]}, 
  "query": {
    "match": {
      "question": "淘寶"
    }
  }
}

排序

GET test_index/test_type/_search
{
  "sort": [
    {
      "askTimes": {
        "order": "desc" } } ], 
  "query": {
    "match_all": {}
  }
}

前綴匹配

GET test_index/test_type/_search
{
  "query": {
    "match_phrase_prefix": {
      "question": "萬科"
    }
  }
}

返回查詢

GET test_index/test_type/_search
{
  "query": {
    "range": {
      "askTimes": {
        "gte": 10, #
        "lte": 20 } }
  }
}

wildcard查詢
*代表0個或多個字符
?代表任意一個字符

GET test_index/test_type/_search
{
  "query": {
    "wildcard": {
      "question":  "萬科*"
    }
  }
}

模糊查詢

GET test_index/test_type/_search
{
  "query": {
    "fuzzy": {"question":  "萬科"}
  }
}

高亮搜索結果

注:在Kibana中沒有看到效果

GET test_index/test_type/_search
{
  "query": {
    "match": {
      "question": "淘寶"
    }
  },
  "highlight": { "fields": {"question":{}} }
}

過濾查詢

GET test_index/test_type/_search
{
  "post_filter": {
    "term": {
      "askTimes": 10
    }
  }
}

過濾非空

GET test_index/test_type/_search
{
  "query": {
    "bool": { "filter": { "exists": { "field": "nlp" } } }
  }
}

聚合查詢

sum,min,max,avg,cardinality:求基數,terms:分組

GET /test_index/test_type/_search
{
  "size": 0, 
  "aggs": {
    "askTimes_of_max": { #自定義名稱
      "max": { #最大值
        "field": "askTimes" } } }
}

 

 

符合查詢:待整理

7、檢查noop更新

默認情況下,不更改任何內容的更新會返回“result”:“noop”;

可以通過設置“detect_noop”來禁用此行為:false

 

PUT test_index/test_type/4
{
  "code":"004",
  "name":"中國平安保險(集團)股份有限公司",
  "count":2,
  "tags":["aa","bb"]
}

 

POST test_index/test_type/4/_update
{
  "doc": {
    "name":"中國平安保險(集團)股份有限公司"
  },
  "detect_noop":false
}

區別:
 
禁用此行為后,不更改任何內容的更新也會返回updated並且文檔版本號加1;
 
不禁用此行為,不更改任何內容的更新會返回noop並且文檔版本號不變。
 
8、Upserts更新
如果文檔不存在,則upsert元素的內容將作為新文檔插入。如果文檔存在,那么script將執行:

POST test_index/test_type/5
{
  "script" : {
    "source": "ctx._source.count += params.count",
    "lang": "painless",
    "params" : {
      "count" : 4
    }
  },
  "upsert" : {
    "count" : 1
  }
}

如果無論文檔是否存在您都希望腳本運行,即腳本處理初始化文檔而不是upsert元素,設置scripted_upsert為true:

POST test_index/test_type/5
{
  "scripted_upsert":true,
  "script" : {
    "source": "ctx._source.count += params.count",
    "lang": "painless",
    "params" : {
      "count" : 4
    }
  },
  "upsert" : {
    "count" : 1
  }
}

 同scripted_upsert,如果無論文檔是否存在都希望腳本運行,即腳本處理初始化文檔而不是upsert元素,設置doc_as_upsert為true(文檔5不存在):

POST test_index/test_type/5/_update
{
  "doc":{
    "count":12
  },
  "doc_as_upsert":true,
  "upsert":{
    "count":16
  }
}

查詢結果:GET test_index/test_type/5

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "5",
  "_version": 11,
  "found": true,
  "_source": {
    "count": 12
  }
}

 9、其他

①、統計索引文檔數量

描述:GET 索引/類型/_count

GET test_index/test_type/_count 

②檢測分詞效果

POST test_index/_analyze
{
"analyzer": "ik_max_word",
"text": "我是中國人"
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM