【问题标题】:Giving more weight to existence of a field赋予领域的存在更多的权重
【发布时间】:2018-12-04 21:45:24
【问题描述】:

我正在尝试学习和编写弹性搜索查询。我意识到有一个“存在”字段返回指定字段的文档是否存在。为了了解我编写了一个简单的查询,我想了解更多信息并使用查询结构。

我有一个查询,只需检查至少一个指定字段是否存在。但是,我想给一个领域更多的权重。这是我的查询:

"query": {
"bool": {
  "minimum_should_match" : 1,
  "should": [
    {
      "exists": {
        "field": "geo"
      }
    },
    {
      "exists": {
        "field": "location"
      }
    }
  ]
   "size": 100
}

我想先获取所有包含 geo 字段的文档(例如有 30 个包含 location 字段的文档),其余 70 个(大小 - 文档存在 geo 字段)将包含包含 location 字段的文档(其他应该) .因此,对于我的情况,位置字段权重的存在小于地理存在。

我为此尝试了 boost,但当我这样做时,它对我的​​情况不起作用;

"query": {
"bool": {
  "minimum_should_match" : 1,
  "should": [
    {
      "exists": {
        "field": "geo",
        "boost": 5 
      }
    },
    {
      "exists": {
        "field": "location"
      }
    }
  ]
   "size": 100
}

当我将 minimum_should_match 更改为 2 时,它只返回存在地理字段的文档。

【问题讨论】:

    标签: elasticsearch lucene


    【解决方案1】:

    在这种情况下,您不应该使用 boost。改用排序:

    "query": {
      "bool": {
        "minimum_should_match" : 1,
        "should": [
          {
            "exists": {
              "field": "geo"
            }
          },
          {
            "exists": {
              "field": "location"
            }
          }
        ]
      "size": 100
      }
    },
    "sort" : [
      { "geo" : {"order" : "asc"}},
      { "location" : {"order" : "asc"}}
    ]
    

    这样您将获得排序的结果(首先是带有地理字段的文档,然后是带有位置字段的文档)

    【讨论】:

      【解决方案2】:

      你应该试试这个查询

      {
        "query": {
          "function_score": {
            "functions": [
              {
                "filter": {
                  "exists": {
                    "field": "geo"
                  }
                },
                "weight": 2
              },
              {
                "filter": {
                  "exists": {
                    "field": "location"
                  }
                },
                "weight": 1
              }
            ]
          }
        },
        "from": 0,
        "_source": [
          "geo", "location"
        ],
        "size": 100
      }
      

      这给出了以下结果;

       {
          "_index": "mentions",
          "_type": "post",
          "_id": "1",
          "_score": 2,
          "_source": {
            "geo": {
              "lon": XXX,
              "lat": XXX
            },
            "location": "California, USA"
          }
        },
      
      {
          "_index": "mentions",
          "_type": "post",
          "_id": "2",
          "_score": 1,
          "_source": {
            "location": "Berlin, Germany"
          }
        }
      

      第一个的函数得分为 2,因为它有一个地理字段,但第二个没有。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-03
        • 2020-10-01
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 2019-11-12
        • 1970-01-01
        相关资源
        最近更新 更多