全文检索完全匹配高亮查询
全文检索请求方式get
请求url: http://127.0.0.1:9200/shopping/_search
请求体执行如下的请求:
{ "query":{ "match":{ "category":"小华" } } }
得到的查询结果如下:
{ "took": 13, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": 0.8266786, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "SJtEPn4BwYTyfKb722JN", "_score": 0.8266786, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 6999.00 } }, { "_index": "shopping", "_type": "_doc", "_id": "SZtEPn4BwYTyfKb78GIE", "_score": 0.8266786, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 7999.00 } }, { "_index": "shopping", "_type": "_doc", "_id": "SptFPn4BwYTyfKb7AGLp", "_score": 0.8266786, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 1999.00 } }, { "_index": "shopping", "_type": "_doc", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米手机", "category": "小米", "images": "http://xiaomi.com", "price": 1 } }, { "_index": "shopping", "_type": "_doc", "_id": "ex0KPX4BPkzBbPhZJmdM", "_score": 0.5753642, "_source": { "title": "小米手机", "category": "小米", "images": "http://xiaomi.com", "price": 3999.00 } }, { "_index": "shopping", "_type": "_doc", "_id": "fB0NPX4BPkzBbPhZNmcn", "_score": 0.5753642, "_source": { "title": "小米手机", "category": "小米", "images": "http://xiaomi.com", "price": 3999.00 } }, { "_index": "shopping", "_type": "_doc", "_id": "2", "_score": 0.5753642, "_source": { "title": "小米手机", "category": "小米", "images": "http://xiaomi.com", "price": 3999.00 } } ] } }
可以看到是把所有的数据都 查询出来了.
然而category并没有"小华"
原因是因为es默认的分词器, 是把中文按照一个一个汉字进行分词,放入倒排索引中的, 小米和华为, 分别包含小和华. 在查询的时候, 把小华也是进行一个一个汉字 分词去查询的, 因此查询出来了所有的数据 .
完全匹配要用match_phrase , 而不是match
请求体执行如下的请求:
{ "query":{ "match_phrase":{ "category":"小华" } } }
结果如下:
{ "took": 22, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] } }
只有如下完全匹配, 才能查询到数据
{ "query":{ "match_phrase":{ "category":"华为" } } }高亮查询
使用highlight 进行高亮的结果显示.
案例
{ "query":{ "match_phrase":{ "category":"华为" } }, "highlight": { "fields": { "category": {} } } }
对category 进行高亮
结果如下
{ "took": 119, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.6533571, "hits": [ { "_index": "shopping", "_type": "_doc", "_id": "SJtEPn4BwYTyfKb722JN", "_score": 1.6533571, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 6999.00 }, "highlight": { "category": [ "华为" ] } }, { "_index": "shopping", "_type": "_doc", "_id": "SZtEPn4BwYTyfKb78GIE", "_score": 1.6533571, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 7999.00 }, "highlight": { "category": [ "华为" ] } }, { "_index": "shopping", "_type": "_doc", "_id": "SptFPn4BwYTyfKb7AGLp", "_score": 1.6533571, "_source": { "title": "华为手机", "category": "华为", "images": "http://huawei.com", "price": 1999.00 }, "highlight": { "category": [ "华为" ] } } ] } }
使用 进行高亮
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)