################Query & Filtering 与多字符串多字段查询############
DELETE products
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }
# bool查询 是一个或者多个查询字句的组合 子查询可以任意顺序出现,可以嵌套多个查询,如果你的bool查询中,没有must查询,则should中比喻至少满足一条查询must 和shoud 会计算得分,must_not和filter不会计算得分
## 查询价格=30 不小于10 avaliable 为ture 产品id为指定id的数据
POST products/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"price": {
"value": "30"
}
}
}
],
"filter": {
"term": {
"avaliable": "true"
}
},
"must_not": [
{
"range": {
"FIELD": {
"lte": 10
}
}
}
],
"should": [
{
"term": {
"productID.keyword": {
"value": "JODL-X-1937-#pV7"
}
}
},
{
"term": {
"productID.keyword": {
"value": "XHDK-A-1293-#fJ3"
}
}
}
],
"minimum_should_match": 1
}
}
}
在结构化查询中,对于数组性的查询,查询结果是包含而不是相等
可以通过增加count字段,使用bool查询解决
###改变数据模型,增加字段。解决数组包含而不是精确匹配的问题
POST /newmovies/_bulk
{ "index": { "_id": 1 }}
{ "title" : "Father of the Bridge Part II","year":1995, "genre":"Comedy","genre_count":1 }
{ "index": { "_id": 2 }}
{ "title" : "Dave","year":1993,"genre":["Comedy","Romance"],"genre_count":2 }
### genre_count用来标识该词组中有多少个次,现在我们就可以准确的找到词组中只有一个词的包含comedy的数据
POST newmovies/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"genre.keyword": {
"value": "Comedy"
}
}
},
{
"term": {
"genre_count": {
"value": "1"
}
}
}
]
}
}
}
filter context不参加算分,可以看到score是0
POST newmovies/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"genre.keyword": {
"value": "Comedy"
}
}
},
{
"term": {
"genre_count": {
"value": "1"
}
}
}
]
}
}
}
qeury context 影响得分
POST newmovies/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"genre.keyword": {
"value": "Comedy"
}
}
},
{
"term": {
"genre_count": {
"value": "1"
}
}
}
]
}
}
}
bool query 可以嵌套使用,可以实现should not 逻辑
查询出price=30 并且value不为false的数据
POST products/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"price": {
"value": "30"
}
}
}
],
"should": [
{
"bool": {
"must_not": [
{
"term": {
"avaliable": {
"value": "false"
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
查询语句的结构,会对相关度算分产生影响
同一层级下的竞争字段,具有相同的权重
通过嵌套bool查询,可以改变对算法的影响
POST /animals/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"text": "brown"
}
},
{
"term": {
"text": "red"
}
},
{
"term": {
"text": "quick"
}
},
{
"term": {
"text": "dog"
}
}
]
}
}
}
POST /animals/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"text": "quick"
}
},
{
"term": {
"text": "dog"
}
},
{
"bool": {
"should": [
{
"term": {
"text": "brown"
}
},
{
"term": {
"text": "brown"
}
}
]
}
}
]
}
}
}
控制字段的bootsing
bootsing是控制相关度的一种手段
DELETE blogs
POST /blogs/_bulk
{ "index": { "_id": 1 }}
{"title":"Apple iPad", "content":"Apple iPad,Apple iPad" }
{ "index": { "_id": 2 }}
{"title":"Apple iPad,Apple iPad", "content":"Apple iPad" }
##● 参数 boost的含义
##● 当 boost > 1 时,打分的相关度相对性提升
##● 当 0 < boost < 1 时,打分的权重相对性降低
##● 当 boost < 0 时,贡献负分
## 例如:我们可以指定 title中的boost分数更高一些,这样id为2的数据就会拍到前面
POST blogs/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "apple,ipad",
"boost": 10
}
}
},
{
"match": {
"content": {
"query": "apple,ipad",
"boost": 1
}
}
}
]
}
}
}
## 当指定content中的boost分更高一些的时候,id为1的数据就会拍到前面
POST blogs/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "apple,ipad",
"boost": 1
}
}
},
{
"match": {
"content": {
"query": "apple,ipad",
"boost": 10
}
}
}
]
}
}
}
要求查询出关于苹果产品的数据,而不需要苹果公司的数据
通过boolquery实现必须不包含
DELETE news
POST /news/_bulk
{ "index": { "_id": 1 }}
{ "content":"Apple Mac" }
{ "index": { "_id": 2 }}
{ "content":"Apple iPad" }
{ "index": { "_id": 3 }}
{ "content":"Apple employee like Apple Pie and Apple Juice" }
POST news/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "apple"
}
}
],
"must_not": [
{
"match": {
"content": "pie"
}
}
]
}
}
}
boosting query 关键字/推进查询
positive 提高查询的分数 negative + negative 进行分数的结果影响
如果仅仅是想让结果排名靠后,可以通过boost 设置得分,也可以使用boosting query
POST news/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "apple"
}
},
"negative": {
"match": {
"content": "pie"
}
},
"negative_boost": 0.2
}
}
}