我的索引中有3个文档
文档1:
X:1
文档2:
X:1 y:1
文档3:
X:2 y:3
我正在使用这个查询来查找相关文档
"query" : {
"bool" : {
"should" : [
{
"match" : {
"X" : {
"query" : 1,
"boost" : 500
}
}
},{
"match" : {
"Y" : 5
}
}
]
}
}当我查找x:1,y:1时,这个查询的工作方式与我预期的一样。我得到了得分最高的DOC2。
我试图实现的是:当查询是x:1,y:5时,我希望获得DOC1 (默认)。有可能吗?
我知道我可以通过添加“AND NOT EXISTS”查询来实现它。还有别的办法吗?
发布于 2020-05-20 17:12:53
强制x并使y成为自愿的怎么样,例如:
GET xy/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"x": {
"query": 1,
"boost": 500
}
}
}
],
"should": [
{
"match": {
"y": 5
}
}
]
}
}
}发布于 2020-06-08 16:56:46
简单地玩一下bool语句怎么样?
它使您选择文档1(默认),如果您找到正确的Y值,则在该值存在的情况下提高分数。即:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must" : [
{
"match" : {
"x" : {
"query" : 1,
"boost" : 500
}
}
},
{
"bool" :
{
"must_not" : {
"exists" : {
"field": "y"
}
}
}
}
]
}
},
{
"bool": {
"must" : [
{
"match" : {
"x" : {
"query" : 1
}
}
},
{
"match" : {
"y" : {
"query" : 5,
"boost" : 600
}
}
}
]
}
}
]
}
}
}您还可以使用function_score语句:
{
"query" : {
"function_score": {
"query" : {
"bool" : {
"should" : [
{
"match" : {
"x" : {
"query" : 1,
"boost": 10
}
}
},{
"match" : {
"y" : {
"query": 5,
"boost": 20
}
}
}
]
}
},
"functions": [
{
"filter": { "match": { "x": "1" } },
"weight": 1
},
{
"filter": {"bool": {"must": { "match": { "y": "5" } } } },
"weight": 10
},
{
"filter": {"bool": {"must_not": { "exists": { "field": "y" } } } },
"weight": 1
}
],
"score_mode": "sum",
"boost_mode": "sum"
}
}
}https://stackoverflow.com/questions/61907640
复制相似问题