ES基礎五 多個索引精確命中個數 查詢


POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"title" : "this is java and elasticsearch blog"} }
{ "update": { "_id": "2"} }
{ "doc" : {"title" : "this is java blog"} }
{ "update": { "_id": "3"} }
{ "doc" : {"title" : "this is elasticsearch blog"} }
{ "update": { "_id": "4"} }
{ "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }

 


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

/java或elasticsearch
GET /forum/article/_search
{"query": {"match": {
  "title": "java elasticsearch"
}}
}
/java和elasticsearch GET
/forum/article/_search { "query": { "constant_score": { "filter": { "match": { "title": { "query": "java elasticsearch", "operator": "and" } } } } } } /java 和elasticsearch GET /forum/article/_search { "query": { "match": { "title": { "query": " elasticsearch java", "operator": "and" } } } } /至少包含三個 GET /forum/article/_search { "query": { "match": { "title": { "query": "java elasticsearch spark hadoop", "minimum_should_match":"75%" } } } } /bool查詢時,如果有must,should中可以不包含任何單詞,但是should匹配會提高score GET /forum/article/_search { "query": { "bool": { "must": { "match": { "title": "java" }}, "must_not": { "match": { "title": "spark" }}, "should": [ { "match": { "title": "hadoop" }}, { "match": { "title": "elasticsearch" }} ] } } } /精確到分詞命中具體個數 GET /forum/article/_search { "query": { "bool": { "should": [ { "match": { "title": "java" } }, { "match": { "title": "elasticsearch" } }, { "match": { "title": "hadoop" } }, { "match": { "title": "spark" } } ], "minimum_should_match":3 } } }

 多個條件如何轉為bool查詢

1、普通match如何轉換為term+should

{
    "match": { "title": "java elasticsearch"}
}

使用諸如上面的match query進行多值搜索的時候,es會在底層自動將這個match query轉換為bool的語法
bool should,指定多個搜索詞,同時使用term query

{
  "bool": {
    "should": [
      { "term": { "title": "java" }},
      { "term": { "title": "elasticsearch"   }}
    ]
  }
}

2、and match如何轉換為term+must

{
    "match": {
        "title": {
            "query":    "java elasticsearch",
            "operator": "and"
        }
    }
}

{
  "bool": {
    "must": [
      { "term": { "title": "java" }},
      { "term": { "title": "elasticsearch"   }}
    ]
  }
}

3、minimum_should_match如何轉換

{
    "match": {
        "title": {
            "query":                "java elasticsearch hadoop spark",
            "minimum_should_match": "75%"
        }
    }
}

{
  "bool": {
    "should": [
      { "term": { "title": "java" }},
      { "term": { "title": "elasticsearch"   }},
      { "term": { "title": "hadoop" }},
      { "term": { "title": "spark" }}
    ],
    "minimum_should_match": 3 
  }
}

 boost,可以將某個搜索條件的權重加大,下面的搜索可以看出elasticsearch 的score比較大。

GET /forum/article/_search 
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "blog"
          }
        }
      ],
      "should": [
        {
          "match": {
            "title": {
              "query": "java"
            }
          }
        },
        {
          "match": {
            "title": {
              "query": "hadoop"
            }
          }
        },
        {
          "match": {
            "title": {
              "query": "elasticsearch",
              "boost":5
            }
          }
        } 
      ]
    }
  }
}

 


免責聲明!

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



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