#filter查询是不计算下相关性的
#同时可以cache,filter速度要快于query
准备数据
POST /lib2/item/_bulk
{"index":{"_id":1}}
{"price":40,"itemID":"10301"}
{"index":{"_id":2}}
{"price":50,"itemID":"10302"}
{"index":{"_id":3}}
{"price":60,"itemID":"10303"}
{"index":{"_id":4}}
{"price":70,"itemID":"10304"}
{"index":{"_id":5}}
{"price":80,"itemID":"10305"}
GET /lib2/item/_search
{
"query": {
"bool": {
"filter": {
"term": {
"price":40
}
}
}
}
}
#terms不支持参数是数组式的查询
GET /lib2/item/_search
{
"query": {
"bool": {
"filter": {
"terms": {
"price":[40,50]
}
}
}
}
}
#动态创建mapping是,文本类型的数据,在存储的时候会被转化成小写,查询的时候也要写成小写
#bool过滤查询
#可以实现组合过滤查询
#格式:{"bool":{"must":[],"should":[],"must_not":[]}}
#must:必须满足的条件-----and
#should:可以满足也可以不满足的条件---or
#must_not:不需要满足的条件--not
GET /lib2/item/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"price": 40
}
},
{
"term": {
"itemID": "10302"
}
}
],
"must_not": [
{
"term": {
"price": 60
}
}
]
}
}
}
#查询itemID为10301或者(itemID为10302且price为50)
GET /lib2/item/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"itemID": "10301"
}
},
{
"bool": {
"must": [
{
"term": {
"itemID": "10302"
}
},
{
"term": {
"price": 50
}
}
]
}
}
]
}
}
}
#范围查询
# gt: >
# lt: <
# gte: >=
# lte: <=
GET /lib2/item/_search
{
"query": {
"bool": {
"filter": {
"range": {
"price": {
"gt":40,
"lt":60
}
}
}
}
}
}
#查询price属性不为null的文档
GET /lib2/item/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "price"
}
}
}
}
}