简体   繁体   English

弹性搜索 - 默认文档

[英]elastic search - default documents

I have 3 documents in my index我的索引中有 3 个文档

document 1:文件1:

x: 1 ×:1

document 2:文件 2:

x: 1 y: 1 x: 1 y: 1

document 3:文件 3:

x: 2 y: 3 x: 2 y: 3

I am using this query to find relevant documents我正在使用此查询来查找相关文档

"query" : {
        "bool" : {
            "should" : [

                {
                    "match" : {
                        "X" : {
                            "query" : 1,
                            "boost" : 500
                        }
                    }
                },{
                    "match" : {
                       "Y" : 5
                    }
                }
            ]
        }
    }

this query works as I expect when I look for x:1, y:1 I get DOC2 with a highest score.当我查找 x:1, y:1 时,此查询按预期工作,我得到的 DOC2 得分最高。

what I try to achieve: when the query is x:1, y:5 I want to get the DOC1 (as default).我试图实现的目标:当查询是 x:1, y:5 我想获得 DOC1(默认)。 is it possible?可能吗?

I know that I can achieve it by adding queries 'AND NOT EXISTS'.我知道我可以通过添加查询“AND NOT EXISTS”来实现它。 is there another way?还有其他方法吗?

How about forcing the x and making y voluntary, ie:强迫x并使y自愿怎么样,即:

GET xy/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "x": {
              "query": 1,
              "boost": 500
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "y": 5
          }
        }
      ]
    }
  }
}

How about simply playing with bool statements?简单地玩布尔语句怎么样?

It makes you selecting the doc 1 (default), and if you find the correct value of Y, you boost the score if the value exists.它使您选择 doc 1(默认),并且如果您找到正确的 Y 值,则如果该值存在,则可以提高分数。 ie: IE:

{
  "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
                        }
                    }
                }
            ]
          }
        }
      ]
    }
  }
}

Also you can use function_score statement:您也可以使用 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"
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM