ES 全文搜索

ES 全文搜索

  1. 全文搜索
GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": "BROWN DOG!"
        }
    }
}

使用了match查詢的多詞查詢只是簡單地將生成的term查詢包含在了一個bool查詢中。通過默認的or操作符,每個term查詢都以一個語句被添加,所以至少一個should語句需要被匹配。以下兩個查詢是等價的:

{
    "match": { "title": "brown fox"}
}
{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}
  1. 提高查詢精度
    match查詢接受一個operator參數,該參數的默認值是"or"。你可以將它改變為"and"來要求所有的詞條都需要被匹配:
GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": {      
                "query":    "BROWN DOG!",
                "operator": "and"
            }
        }
    }
}

使用and操作符時,所有的term查詢都以must語句被添加,因此所有的查詢都需要匹配。以下兩個查詢是等價的:

{
    "match": {
        "title": {
            "query":    "brown fox",
            "operator": "and"
        }
    }
}
{
  "bool": {
    "must": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}
  1. 控制查詢精度
    match查詢支持minimum_should_match參數,它能夠讓你指定有多少詞條必須被匹配才會讓該文檔被當做一個相關的文檔。盡管你能夠指定一個詞條的絕對數量,但是通常指定一個百分比會更有意義,因為你無法控制用戶會輸入多少個詞條:
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": {
        "query":                "quick brown dog",
        "minimum_should_match": "75%"
      }
    }
  }
}

如果指定了minimum_should_match參數,它會直接被傳入到bool查詢中,因此下面兩個查詢是等價的:

{
    "match": {
        "title": {
            "query":                "quick brown fox",
            "minimum_should_match": "75%"
        }
    }
}
{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }},
      { "term": { "title": "quick" }}
    ],
    "minimum_should_match": 2 
  }
}
  1. 合并查詢
    bool查詢通過must,must_not以及should參數來接受多個查詢。比如:
GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "must":     { "match": { "title": "quick" }},
      "must_not": { "match": { "title": "lazy"  }},
      "should": [
                  { "match": { "title": "brown" }},
                  { "match": { "title": "dog"   }}
      ]
    }
  }
}
  1. 控制精度
    正如可以控制match查詢的精度,也能夠通過minimum_should_match參數來控制should語句需要匹配的數量,該參數可以是一個絕對數值或者一個百分比:
GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title": "brown" }},
        { "match": { "title": "fox"   }},
        { "match": { "title": "dog"   }}
      ],
      "minimum_should_match": 2 
    }
  }
}
  1. 提升查詢子句(Boosting Query Clause)
    假設我們需要搜索和"full-text search"相關的文檔,但是我們想要給予那些提到了"Elasticsearch"或者"Lucene"的文檔更多權重。更多權重的意思是,對于提到了"Elasticsearch"或者"Lucene"的文檔,它們的相關度_score會更高,即它們會出現在結果列表的前面。
    一個簡單的bool查詢能夠讓我們表達較為復雜的邏輯:
GET /_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "content": { 
                        "query":    "full text search",
                        "operator": "and"
                    }
                }
            },
            "should": [ 
                { "match": { "content": "Elasticsearch" }},
                { "match": { "content": "Lucene"        }}
            ]
        }
    }
}
  1. 多個查詢字符串(Multiple Query Strings)
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }}
      ]
    }
  }
}
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }},
        { "bool":  {
          "should": [
            { "match": { "translator": "Constance Garnett" }},
            { "match": { "translator": "Louise Maude"      }}
          ]
        }}
      ]
    }
  }
}
  1. 設置子句優先級
    通過boost參數,增加字段的權重
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { 
            "title":  {
              "query": "War and Peace",
              "boost": 2
        }}},
        { "match": { 
            "author":  {
              "query": "Leo Tolstoy",
              "boost": 2
        }}},
        { "bool":  { 
            "should": [
              { "match": { "translator": "Constance Garnett" }},
              { "match": { "translator": "Louise Maude"      }}
            ]
        }}
      ]
    }
  }
}
  1. 多字段查詢
{
    "multi_match": {
        "query":                "Quick brown fox",
        "type":                 "best_fields", 
        "fields":               [ "title", "body" ],
        "tie_breaker":          0.3,
        "minimum_should_match": "30%" 
    }
}

注意到以上的type屬性為best_fields、minimum_should_match和operator參數會被傳入到生成的match查詢中。
在字段名中使用通配符

{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": "*_title"
    }
}

提升個別字段
個別字段可以通過caret語法()進行提升:僅需要在字段名后添加boost,其中的boost是一個浮點數:

{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": [ "*_title", "chapter_title^2" ] 
    }
}

同一個字段使用了不同的解析器的情況

GET /my_index/_search
{
   "query": {
        "multi_match": {
            "query":  "jumping rabbits",
            "type":   "most_fields", 
            "fields": [ "title", "title.std" ]
        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容