Elasticsearch基本概念和使用(4)

结果:

{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "2", "_score": 1, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } }, { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } } ] } }

took:查询花费时间,单位是毫秒

time_out:是否超时

_shards:分片信息

hits:搜索结果总览对象

total:搜索到的总条数

max_score:所有结果中文档得分的最高分

hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息

_index:索引库

_type:文档类型

_id:文档id

_score:文档得分

_source:文档的源数据

2.1.2 匹配查询(match)

我们先加入一条数据,便于测试:

PUT /heima/goods/3 { "title":"小米电视4A", "images":"http://image.leyou.com/12479122.jpg", "price":3899.00 }

现在,索引库中有2部手机,1台电视:

Elasticsearch基本概念和使用

or关系

match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系

GET /heima/_search { "query":{ "match":{ "title":"小米电视" } } }

结果:

"hits": { "total": 2, "max_score": 0.6931472, "hits": [ { "_index": "heima", "_type": "goods", "_id": "tmUBomQB_mwm6wH_EC1-", "_score": 0.6931472, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } }, { "_index": "heima", "_type": "goods", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米电视4A", "images": "http://image.leyou.com/12479122.jpg", "price": 3899 } } ] }

在上面的案例中,不仅会查询到电视,而且与小米相关的都会查询到,多个词之间是or的关系。

and关系

某些情况下,我们需要更精确查找,我们希望这个关系变成and,可以这样做:

GET /heima/_search { "query":{ "match": { "title": { "query": "小米电视", "operator": "and" } } } }

结果:

{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "heima", "_type": "goods", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米电视4A", "images": "http://image.leyou.com/12479122.jpg", "price": 3899 } } ] } }

本例中,只有同时包含小米和电视的词条才会被搜索到。

or和and之间?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11604.html