elasticsearch dsl 經常使用過濾和查詢語句

過濾語句爲 從全部數據中查找一個結果集,查詢語句則是標示的是查找一個精確的結果集合信息;sql

查詢語句會詢問每一個文檔的字段值與特定值的匹配程度如何數組

一條過濾語句會詢問每一個文檔的字段值是否包含着特定值:curl

一條查詢語句會計算每一個文檔與查詢語句的相關性,會給出一個相關性評分 _score ,而且 按照相關性對匹配到的文檔進行nosql

排序。 這種評分方式很是適用於一個沒有徹底配置結果的全文本搜索url

注:紅色部分沒有明白什麼意思,若是有知道的煩請給予幫助,謝謝。。。。spa

一、索引中的數據爲:code

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "dsltest",
        "_type" : "dsltesttype",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "age" : 30,
          "date" : "2017-09-01",
          "public" : true,
          "tag" : [
            "full_text"
          ],
          "title" : "how to make second"
        }
      },
      {
        "_index" : "dsltest",
        "_type" : "dsltesttype",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "age" : 26,
          "date" : "2014-09-01",
          "public" : true,
          "tag" : [
            "full_text",
            "nosql"
          ],
          "title" : "how to make millions"
        }
      }
    ]
  }
}

過濾語句:對象

term 過濾的是單個字段,用的是對象的方式,若是多個字段會報 [term] query does not support array of values term爲過濾語句,因此不能添加match匹配信息排序

語法:
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"term":{"age":26}}}'

terms 過濾的是多個字段,用的是數組的方式;若是用對象的方式則會報錯索引

terms中數組中的多個字段 標示的是 或者(OR)的關係

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"terms":{"title":["make"]}}}'

range 查找範圍內的數據:包括 gt大於  gte 大於等於  lt小於 lte小於等於

查找 年齡大於等於30的數據
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"range":{"age":{"gte":30}}}}'

exists 查找包含 某一個字段的數據 

獲取包含title字段的數據
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"exists":{"field":"title"}}}'

若是存在多個過濾條件則經過 bool進行處理

must :: 多個查詢條件的徹底匹配,至關於 and 。

must_not :: 多個查詢條件的相反匹配,至關於 not 。

should :: 至少有一個查詢條件匹配, 至關於 or 。

多個過濾條件使用的方式爲:

表示的是:過濾title中包含 make 同時age大於等於 27的數據
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"bool":{"must":{"term":{"title":"make"}},"must":{"range":{"age":{"gte":27}}}}}}'

查詢語句:

match_all 查詢全部的數據

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"match_all":{}}}'
相似於:
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty'

match 查詢匹配對應的詞 對title進行分詞查詢結果

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"match":{"title":"make"}}}'

multi_match 多字段匹配:查找 字段title中爲make的數據,其中fields中的字段不必定存在

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"multi_match":{"query":"make","fields":["title"]}}}'

若是同時存在多個一樣也是用的 bool 查詢bool和過濾bool相同 查詢條件的使用方式爲:

查詢語句中 title中包含 make和second 或者 age包含26的數據
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"bool":{"must":[{"match":{"title":"make"}},{"match":{"title":"second"}}],"should":{"match":{"age":26}}}}}'

同時包含 過濾 條件 和查詢條件的語句方式爲:[查詢的爲:title爲make 同時 age 大於等於26,同時title爲make過濾]

當前版本 5.2.2已經沒有啦 filtered過濾字段,更改成bool 若是對查詢和過濾合併處理的方式爲
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query": {"bool": {"must":{"match":{"title":"make"}},"filter": {"bool": {"must": [{"term": {"title": "make"}}, {"range": {"age": {"gte": 26}}}]}}}}}'
相關文章
相關標籤/搜索