2017-08-25 85 views
3

我正在使用fuzzy並希望elasticsearch返回搜索到的單詞,而不僅僅是搜索結果。 當我搜索單詞dogo和我的模糊搜索發現字dog我想知道它是dogo誰找到它。Elasticsearch返回搜索詞

數據:

{ "index": { "_id":1 }} 
{ "title": "The quick brown fox", "price":5 } 
{ "index": { "_id":2 }} 
{ "title": "The quick blue dog", "price":7 } 
{ "index": { "_id":3 }} 
{ "title": "The slow brown dog", "price":5 } 

查詢:

{ 
    "query": { 
    "bool": { 
    "should": [ 
     { 
      "fuzzy": { 
        "title": "dogo" 
         } 

      }, 
     { 
      "fuzzy": { 
        "title": "fox" 
         } 
      } 
     ] 
    } 

    }, 
    "highlight" : { 
     "fields" : { 
      "title":{ 
       "pre_tags": [ 
       "===>" 
       ], 
       "post_tags": [ 
       "<===" 
       ], 
       "fragment_size": 200, 
       "number_of_fragments": 100 
      } 
     } 
    } 
} 

這個查詢將返回===>dog<===,但不知道是否dogo發現了它。

有誰知道如何做到這一點或想法? 我希望我的輸出結果類似dog : dogo

回答

4

您可以使用named queries爲此,爲每個查詢命名。在結果中,每個匹配將包含一個matched_queries數組,其中包含匹配查詢的名稱(例如,下面的dogofox)。

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "fuzzy": { 
      "name": { 
       "value": "dogo", 
       "_name": "dogo" 
      } 
      } 
     }, 
     { 
      "fuzzy": { 
      "name": { 
       "value": "fox", 
       "_name": "fox" 
      } 
      } 
     } 
     ] 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "title": { 
     "pre_tags": [ 
      "===>" 
     ], 
     "post_tags": [ 
      "<===" 
     ], 
     "fragment_size": 200, 
     "number_of_fragments": 100 
     } 
    } 
    } 
} 
+0

真棒,很高興它幫助! – Val

+0

非常感謝:) – kemis

1

命名查詢是瞭解結果中查詢名稱的正確選擇。如果您想知道查詢字詞的可能更正條款,也可以嘗試建議。

{ 
    "query": { 
    "bool": { 
    "should": [ 
     { 
      "fuzzy": { 
        "title": "dogo" 
         } 

      }, 
     { 
      "fuzzy": { 
        "title": "fox" 
         } 
      } 
     ] 
    } 

    }, 
    "highlight" : { 
     "fields" : { 
      "title":{ 
       "pre_tags": [ 
       "===>" 
       ], 
       "post_tags": [ 
       "<===" 
       ], 
       "fragment_size": 200, 
       "number_of_fragments": 100 
      } 
     } 
    } , 
"suggest" : { 
    "title_suggestion" : { 
     "text" : "fox dogo", 
     "term" : { 
     "field" : "title" 
     } 
    } 
    } 
}