2014-02-13 87 views
5

我正在運行模糊搜索,並且需要查看哪些詞匹配。例如,如果我正在搜索查詢testing,並且該字段與句子The boy was resting匹配,則需要能夠知道該匹配是由於單詞resting所致。確定哪些詞在模糊搜索中匹配

我試着設置參數explain = true,但它似乎並不包含我需要的信息。有什麼想法嗎?

+0

不幸的是,我沒有看到任何模糊文檔..我猜想解釋是你最好的選擇,但它聽起來像它沒有它的天堂。我也很感興趣。 – Sam

+1

@SamSullivan經過多一點研究,我找到了答案。這是一個稱爲突出顯示的功能,我在下面的答案中發佈了這個功能。 – Ari

+0

啊,你真的想要突出顯示? XY-問題。 :)讓我的答案在那裏爲後代。 –

回答

7

好吧,這就是我一直在尋找:

了一些研究之後,我發現elasticsearch的Highlighting feature

默認情況下,它會返回圍繞匹配的上下文片段,但您可以將片段大小設置爲查詢長度以僅返回完全匹配。例如:

{ 
    query : query, 
    highlight : { 
     "fields" : { 
      'text' : { 
       "fragment_size" : query.length 
      } 
     } 
    } 
} 
0

使用explain應該給你一些線索,雖然不是很容易獲得。

如果您執行以下操作,也可在https://www.found.no/play/gist/daa46f0e14273198691a處獲得,您應該看到description: "weight(text:nesting^0.85714287 in 1) […],description: "weight(text:testing in 1) [PerFieldSimilarity] […]等等在命中的_explanation

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{}' 

# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"text":"The boy was resting"} 
{"index":{"_index":"play","_type":"type"}} 
{"text":"The bird was testing while nesting"} 
' 

# Do searches 

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "match": { 
      "text": { 
       "query": "testing", 
       "fuzziness": 1 
      } 
     } 
    }, 
    "explain": true 
} 
'