【问题标题】:Limiting top score results returned based on conditional (same value of a field) ElasticSearch Aggregations根据条件(字段的相同值)限制返回的最高分结果 ElasticSearch Aggregations
【发布时间】:2014-06-06 00:09:46
【问题描述】:

有没有办法根据 _score 只提取在某个字段中具有相同值的文档的前 2 个结果?

此过滤器或聚合之前的点击数如下:

{ 
   "_index":"myindex",
   "_score":100,
   "_source": {
      "myfield1": "i have a twin",
      "name":"fred"
    }
},

{ 
   "_index":"myindex",
   "_score":50,
   "_source": {
      "myfield1": "i have a twin",
      "name":"george"
    }
},

{ 
   "_index":"myindex",
   "_score":10,
   "_source": {
      "myfield1": "i have a twin",
      "name":"tom"
    }
},

{ 
   "_index":"myindex",
   "_score":10,
   "_source": {
      "myfield1": "i DONT have a twin",
      "name":"doug"
    }
}

然后在此过滤器/聚合之后,我希望删除此 ... tom,因为他对 myfield1 具有相同的值,但得分最低。 Doug 留下来是因为他对 myfield1 的价值观不同。

{ 
   "_index":"myindex",
   "_score":100,
   "_source": {
      "myfield1": "i have a twin",
      "name":"fred"
    }
},

{ 
   "_index":"myindex",
   "_score":50,
   "_source": {
      "myfield1": "i have a twin",
      "name":"george"
    }
},

{ 
   "_index":"myindex",
   "_score":10,
   "_source": {
      "myfield1": "i DONT have a twin",
      "name":"doug"
    }
}

我希望这会产生正确的效果......但它没有。我接近了吗?

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "match_phrase_prefix": {
                "myfield1": {
                  "query": "i have",
                  "fuzziness": 1,
                  "slop": 2,
                  "max_expansions": 10,
                  "prefix_length": 1
                }
              }
            }
          ]
        }
      },
      "boost_mode": "replace",
      "functions": [
        {
          "script_score": {
            "script": "_score * [...] "
          }
        }
      ]
    }
  },
  "aggs": {
    "myfield1": {
      "terms": {
        "field": "myfield1",
        "size": 2,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "field": "_doc.score"
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: search lucene elasticsearch search-engine


    【解决方案1】:

    为确保您只获得给定查询的 2 个答案,请确保在 aggs 之外包含参数 "size" : 2(与示例中的 query 和 aggs 处于同一级别)。 size 参数告诉 ES 你想要多少个答案(即你请求多少个搜索结果),你的第一部分应该是 2。所以

    “查询”:{...},

    ...其他的东西...,

    “大小”:2

    对于给定的查询(您的第一次点击),应该获得 2 次点击。

    另外,我不确定,但看起来您基本上想要搜索文档并返回文档(您想要的示例结果表明这一点)。如果是这种情况,您不需要聚合,只需要搜索结果。

    所以基本上你可以尝试使用 multi_search 端点 (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html) 发送两个单独的搜索请求

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 1970-01-01
      • 2013-12-24
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2023-03-07
      相关资源
      最近更新 更多