2015-04-01 52 views
4

你能幫我解決關於特定語言分析器的一些小問題嗎?分析器是否阻止字段突出顯示?

我需要通過查詢字符串搜索文檔並突出顯示匹配的字符串。 這裏是我的映射:

{ 
    "usr": { 
     "properties": { 
      "text0": { 
       "type": "string", 
       "analyzer": "english" 
      }, 
      "text1": { 
       "type": "string" 
      } 
     } 
    } 
} 

注意,對於「text0」中域「英語」分析儀設置,併爲「text1」中的字段默認使用標準的分析。

在我的指數有現在一個文件:

hits": [{ 
    "_index": "tt", 
    "_type": "usr", 
    "_id": "AUxvIPAv84ayQMZV-3Ll", 
    "_score": 1, 
    "_source": { 
     "text0": "highlighted. need to be highlighted.", 
     "text1": "highlighted. need to be highlighted." 
    } 
}] 

考慮以下查詢:

{ 
    "query": { 
     "query_string" : { 
      "query" : "highlighted" 
     } 
    }, 
    "highlight" : { 
     "fields" : { 
      "*" : {} 
     } 
    } 
} 

我所預料的文檔中的每個領域加以強調,但高亮只出現在「text1」字段(其中沒有分析器集合):

"hits": [{ 
    "_type": "usr", 
    "_source": { 
     "text0": "highlighted. need to be highlighted.", 
     "text1": "highlighted. need to be highlighted." 
    }, 
    "_score": 0.19178301, 
    "_index": "tt", 
    "highlight": { 
     "text1": [ 
      "<em>highlighted</em>. need to be <em>highlighted</em>." 
     ] 
    }, 
    "_id": "AUxvIPAv84ayQMZV-3Ll" 
}] 

讓我們考慮以下NG查詢(我的預期「突出」,因爲分析儀匹配「亮點」):

{ 
    "query": { 
     "query_string" : { 
       "query" : "highlight" 
      } 
     }, 
    "highlight" : { 
      "fields" : { 
       "*" : {} 
      } 
     } 
} 

但有響應無HIST可言:(做過英語儀甚至在這裏工作?)

"hits": { 
    "hits": [], 
    "total": 0, 
    "max_score": null 
} 

最後,考慮到一些捲曲的命令(請求和響應):

curl "http://localhost:9200/tt/_analyze?field=text0" -d "highlighted" 

{"tokens":[{ 
    "token":"highlight", 
    "start_offset":0, 
    "end_offset":11, 
    "type":"<ALPHANUM>", 
    "position":1 
}]} 

curl "http://localhost:9200/tt/_analyze?field=text1" -d "highlighted" 

{"tokens":[{ 
    "token":"highlighted", 
    "start_offset":0, 
    "end_offset":11, 
    "type":"<ALPHANUM>", 
    "position":1 
}]} 

我們看到,通過傳遞文本通過英語和標準的分析,其結果是不同的。 最後,問題是:分析器是否防止突出顯示字段?如何在全文搜索時突出顯示我的字段?

P.S.我使用windows 8.1在本地機器上使用elasticsearch v1.4.4。

回答

2

它與您的查詢有關。您正在使用query_string查詢,並且您沒有指定該字段,因此它默認在_all字段上進行搜索。 這就是爲什麼你看到奇怪的結果。查詢更改爲multi_match查詢在這兩個領域的搜索:

{ 
    "query": { 
     "multi_match": { 
      "fields": [ 
       "text1", 
       "text0" 
      ], 
      "query": "highlighted" 
     } 
    }, 
    "highlight": { 
     "fields": { 
      "*": {} 
     } 
    } 
} 

現在突出了這兩個領域的結果將在響應中返回。