示例:
GET /heima/_search { "_source": ["title","price"], "query": { "term": { "price": 2699 } } }返回的结果:
{ "took": 12, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "price": 2699, "title": "小米手机" } } ] } } 2.2.2.指定includes和excludes我们也可以通过:
includes:来指定想要显示的字段
excludes:来指定不想要显示的字段
二者都是可选的。
示例:
GET /heima/_search { "_source": { "includes":["title","price"] }, "query": { "term": { "price": 2699 } } }与下面的结果将是一样的:
GET /heima/_search { "_source": { "excludes": ["images"] }, "query": { "term": { "price": 2699 } } } 2.3 高级查询 2.3.1 布尔组合(bool)bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合
GET /heima/_search { "query":{ "bool":{ "must": { "match": { "title": "大米" }}, "must_not": { "match": { "title": "电视" }}, "should": { "match": { "title": "手机" }} } } }结果:
{ "took": 10, "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": "2", "_score": 0.5753642, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } } ] } } 2.3.2 范围查询(range)range 查询找出那些落在指定区间内的数字或者时间
GET /heima/_search { "query":{ "range": { "price": { "gte": 1000.0, "lt": 2800.00 } } } }range查询允许以下字符:
操作符说明gt 大于
gte 大于等于
lt 小于
lte 小于等于
2.3.3 模糊查询(fuzzy)
我们新增一个商品:
POST /heima/goods/4 { "title":"apple手机", "images":"http://image.leyou.com/12479122.jpg", "price":6899.00 }fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
GET /heima/_search { "query": { "fuzzy": { "title": "appla" } } }上面的查询,也能查询到apple手机
我们可以通过fuzziness来指定允许的编辑距离:
GET /heima/_search { "query": { "fuzzy": { "title": { "value":"appla", "fuzziness":1 } } } } 2.4 过滤(filter)条件查询中进行过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式:
GET /heima/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手机" }}, "filter":{ "range":{"price":{"gt":2000.00,"lt":3800.00}} } } } }注意:filter中还可以再次进行bool组合条件过滤。
无查询条件,直接过滤