{"took": 6,"timed_out": false,"_shards": { ... },"hits": {"total":3,"max_score":1,"hits": [ {"_index":"megacorp","_type":"employee","_id":"3","_score":1,"_source": {"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests": [ "forestry" ] } }, {"_index":"megacorp","_type":"employee","_id":"1","_score":1,"_source": {"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports","music" ] } }, {"_index":"megacorp","_type":"employee","_id":"2","_score":1,"_source": {"first_name":"Jane","last_name":"Smith","age":32,"about":"I like to collect rock albums","interests": [ "music" ] } } ] }}
{..."hits": {"total":2,"max_score":0.30685282,"hits": [ {..."_source": {"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports","music" ] } }, {..."_source": {"first_name":"Jane","last_name":"Smith","age":32,"about":"I like to collect rock albums","interests": [ "music" ] } } ] }}
你会发现我们同样使用了 match 查询来搜索 about 字段中的 rock climbing。我们会得到两个匹配的文档:
{..."hits": {"total":2,"max_score":0.16273327,"hits": [ {..."_score": 0.16273327, <1> "_source": {"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports","music" ] } }, {..."_score": 0.016878016, <1> "_source": {"first_name":"Jane","last_name":"Smith","age":32,"about":"I like to collect rock albums","interests": [ "music" ] } } ] }}
相关评分
通常情况下,Elasticsearch 会通过相关性来排列顺序,第一个结果中,John Smith 的 about 字段中明确地写到 rock climbing。而在 Jane Smith 的 about 字段中,提及到了 rock,但是并没有提及到 climbing,所以后者的 _score 就要比前者的低。
{..."hits": {"total":1,"max_score":0.23013961,"hits": [ {..."_score": 0.23013961,"_source": {"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports","music" ] } } ] }}
当我们运行这个查询后,相同的命中结果会被返回,但是我们会得到一个新的名叫 highlight 的部分。在这里包含了 about 字段中的匹配单词,并且会被 <em></em> HTML字符包裹住:
{..."hits": {"total":1,"max_score":0.23013961,"hits": [ {..."_score": 0.23013961,"_source": {"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports","music" ] },"highlight": {"about": ["I love to go <em>rock</em> <em>climbing</em>"<1> ] } } ] }}