elasticsearch入門使用(三) Query DSL

Elasticsearch Reference [6.2] » Query DSL
參考官方文檔 :https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.htmljavascript

1、組合查詢 Compound queries

  1. Constant Score Query 指定_score分數查詢
GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}
  1. Bool Query 布爾值查詢

must:查詢的條件必須在匹配的文檔中,並計算類似度得分
filter:必須知足條件,不會計算類似度得分
should:知足子條件的一個或者多個,知足的格式能夠經過"minimum_should_match" : 1設置,相似 OR (若是查詢中包filter則至少知足一個should)
must_not:返回的文檔必須不知足條件,相似 NOThtml

tips : 日期格式在添加文檔和搜索的時候加上T,字符串不區分大小寫 如pek
java

GET /stu/_search
{
   "query": { 
    "bool": { 
      "must": [
        { "match": { "address":"上海市 保德路 閘北區"}}
      ],
      "filter": [
        {"term":{ "id": 11 }},
        {"term":{ "city": "pek" }},
        {"range":{"regdate": {"gte": "2018-03-03T15:33:32","lte":"2018-03-03T15:33:33"}}} 
      ],
      "should":[
        {"term":{ "score": 80.8 }},
        {"term":{ "score": 80.0 }}
      ],
      "must_not":[{
        "term" : { "age" : 30 }
      }],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}

2、查詢上下文

Query Context:文檔和查詢條件的匹配度,出了決定是否與文檔匹配外,還會計算查詢條件和文檔的匹配度_score緩存

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

3、過濾上下文

Filter context: 精確搜索文檔和查詢是否匹配,不會去計算匹配度,主要用於過濾結構化數據.
常常使用的過濾器會被elasticsearch自動緩存,以提升查詢效率elasticsearch

示例:title中包含search、content中包含elasticsearch 且 status="published" & publish_date >="2015-01-01"
filter裏的term、rangeide

GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"}}, 
        { "match": { "content": "Elasticsearch" }}  
      ],
      "filter": [ 
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}

4、查詢全部

查詢全部_score的文檔,(注:boost的默認值是 1.0)ui

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

查詢_score=1.2的全部文檔this

GET /_search
{
    "query": {
        "match_all": { "boost" : 1.2 }
    }
}

5、全文查詢

1.match querycode

query:查詢字段message中包含 " this is test " , 注意 "to be or not to be" 屬於停頓詞,過濾器默認會把remove調,設置zero_terms_query:"all"啓用
另operator/zero_terms_query非必填參數,更詳細內容查看match query官方文檔htm

GET /_search
{
    "query": {
        "match" : {
            "message" : {
                "query" : "to be or not to be,this is test",
                "operator" : "and",
                "zero_terms_query": "all"
            }
        }
    }
}

2.Match Phrase Query

match_phrase從分析文本"this is a test"中建立一組詞去查詢,analyzer分詞器也可使用ik等中文分詞器

GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : {
                "query" : "this is a test",
                "analyzer" : "standard"
            }
        }
    }
}

3.Match Phrase Prefix Query

match_phrase_prefix與match_phrase相似,只是它容許在文本中的最後一個詞的前綴匹配

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
        }
    }
}

4.Multi Match Query

multi_match匹配查詢上以容許多字段查詢(subject/message字段):

GET /_search
{
  "query": {
    "multi_match" : {
      "query":    "this is a test", 
      "fields": [ "subject", "message" ] 
    }
  }
}

5.Common Terms Query
common:停頓詞配置相關如 " the to be "等

6.Query String Query
query_string:沒理解看官方文檔

7.Simple Query String Query
simple_query_string: 與query_string查詢不一樣的是,simple_query_string查詢永遠不會拋出異常,並放棄查詢的無效部分

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "fields": ["title^5", "body"],
        "default_operator": "and"
    }
  }
}
相關文章
相關標籤/搜索