【问题标题】:elastic search - default documents弹性搜索 - 默认文档
【发布时间】:2020-06-08 18:46:10
【问题描述】:

我的索引中有 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”来实现它。还有其他方法吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    强制x 并让y 自愿怎么样,即:

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

    【讨论】:

    • TY 的答案,但它不会改变任何东西,在这种情况下,我们仍然会得到相同分数的 DOC1 和 DOC2。因为 X:1 存在于它们两者中。当我的第二个术语(Y)根本不存在于文档中时,我想首先获得 DOC1(最高分),并且我寻找如果不存在就不要使用,因为明天我可以有 3-5 个这样的术语。或者也许不存在,但不知何故,如果我有更多的条款,我将不需要为每个条款添加“不存在”查询
    【解决方案2】:

    简单地使用 bool 语句怎么样?

    它让您选择 doc 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"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 2022-08-12
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多