学习查询之前,我还是老规矩,先准备数据
#Filter查询
#filter是不计算相关性的,同时可以cache.因此,filter速度要快于query.
POST /lib5/items/_bulk
{"index":{"_id": 1}}
{"price": 40,"itemID": "ID100123"}
{"index":{"_id": 2}}
{"price": 50,"itemID": "ID100124"}
{"index":{"_id": 3}}
{"price": 25,"itemID": "ID100124"}
{"index":{"_id": 4}}
{"price": 30,"itemID": "ID100125"}
{"index":{"_id":5}}
{"price": null, "itemID": "ID100127"}
查询价格为40的(过滤价格为40的数据)
查询价格25和40的
注意:
我们post请求添加数据我用的是动态映射,自动创建的mapping,我们可以看下mapping
注意:我们看见了itemID是text类型 ,而text类型默认是进行分词的,分词的时候会将 "ID100123" 大写的字母转化成小写字母 "id100123"
我们将改成小写的试试,发现是ok的 如下图所示:
查询语句:
####2.8.1简单的过滤查询
#term
GET /lib5/items/_search
{ "post_filter":{"term":{ "price":40}}
} #terms
GET /lib5/items/_search
{"post_filter":{"terms":{ "price": [25,40]}}
}GET /lib5/items/_search
{"post_filter":{"term": { "itemID": "ID100123"}}
}
GET /lib5/items/_search
{"query": {"bool": {"filter": [{ "term": { "price": 40}}]}}
}GET /lib5/items/_search
{"query": {"bool": {"filter": [{ "terms": { "price": [25,40] }}]}}
}GET /lib5/items/_search
{"query": {"bool": {"filter": [{"term": { "itemID":"5100123" }}]}}
}