一、全文搜索(一种传统数据库很难实现的功能)
我们将会搜索所有喜欢“rock climbing”的员工:GET /megacorp/employee/_search{ "query" : { "match" : { "about" : "rock climbing" } }}
你可以看到我们使用了之前的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, <2> "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ] }}
默认情况下,Elasticsearch根据结果相关性评分来对结果集进行排序, 结果相关性评分就是文档与查询条件的匹配程度。
但是为什么Jane Smith
也会出现在结果里呢?原因是“rock”在她的abuot
字段中被提及了。因为只有“rock”被提及而“climbing”没有,所以她的_score
要低于John。
二、短语搜索
全文搜索允许我们在字段中搜索单独的一个词,这挺好的,但是有时候你想要确切的匹配若干个单词或者短语(phrases)。例如我们想要查询同时包含"rock"和"climbing"(并且是相邻的)的员工记录。
GET /megacorp/employee/_search{ "query" : { "match_phrase" : { "about" : "rock climbing" } }}
毫无疑问,该查询返回John Smith的文档:
{ ... "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" ] } } ] }}
三、查询高亮显示
Elasticsearch中高亮片段是非常容易的,让我们在之前的语句上增加highlight
参数:
{ "query": { "match_phrase": { "company_name": "苗会" } }, "highlight": { "fields": { "company_name": {} } }}
当我们运行这个语句时,会命中与之前相同的结果,但是在返回结果中会有一个新的部分叫做highlight,
并且用<em></em>
来标识匹配到的单词。
{"_index": "miaotu_formal","_type": "company_info_and_type","_id": "234","_score": 3.3942661,"_source": {"company_name": "中苗会","short_name": "中苗会","type_name": "景观设计"},"highlight": {"company_name": ["中苗会"]}}