elasticsearch 更重视场的存在性,elasticsearch,lucene,elasticsearch,Lucene" /> elasticsearch 更重视场的存在性,elasticsearch,lucene,elasticsearch,Lucene" />

elasticsearch 更重视场的存在性

elasticsearch 更重视场的存在性,elasticsearch,lucene,elasticsearch,Lucene,我正在尝试学习和编写elasticsearch查询。我意识到有一个“exists”字段返回指定字段是否存在的文档。为了了解这一点,我编写了一个简单的查询,我想了解更多信息并使用查询结构 我有一个查询,它只检查至少一个指定字段是否存在。然而,我想给一个领域更多的权重。这是我的疑问: "query": { "bool": { "minimum_should_match" : 1, "should": [ { "exists": { "field": "ge

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

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

"query": {
"bool": {
  "minimum_should_match" : 1,
  "should": [
    {
      "exists": {
        "field": "geo"
      }
    },
    {
      "exists": {
        "field": "location"
      }
    }
  ]
   "size": 100
}
我希望首先获取所有具有地理字段的文档(例如,有30个文档包含位置字段),其余70个(大小-文档存在地理字段)将包含位置字段的文档(其他应该)。因此,在我的例子中,位置场权重的存在小于地理存在

我为此尝试了boost,但在我这样做时,它对我的案例不起作用

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

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

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

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

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

您应该尝试此查询

{
  "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,因为它有一个地理场,但第二个没有