【问题标题】:Elasticsearch: Top k results per keywordElasticsearch:每个关键字的前 k 个结果
【发布时间】:2017-12-12 14:05:05
【问题描述】:

我们在elasticsearch中有以下文档。

class Query(DocType):
    text = Text(analyzer='snowball', fields={'raw': Keyword()})
    src = Keyword()

现在我们想要每个 src 的前 k 个结果。我们怎样才能做到这一点?

示例:- 假设我们索引以下内容:

# src: place_order
Query(text="I want to order food", src="place_order")
Query(text="Take my order", src="place_order")
...

# src: payment
Query(text="How to pay ?", src="payment")
Query(text="Do you accept credit card ?", src="payment")
...

现在如果用户写了一个查询take my order please as well as the credit card details,并且k=1,那么我们应该返回以下两个结果

[{"text": "Take my order", "src": "place_order", }, 
 {"text": "Do you accept credit card ?", "src": "payment"}
]

由于 k=1,我们只为每个 src 返回一个结果。

【问题讨论】:

  • 我知道这是你的映射;您能否还提供一些文档示例,以及期望的返回结果是什么?谢谢。
  • @NikolayVasiliev 我已经添加了示例

标签: elasticsearch elasticsearch-5 elasticsearch-dsl


【解决方案1】:

您可以尝试top hits 聚合,它将返回聚合中每个存储桶的前 N ​​个匹配文档。

对于您帖子中的示例,查询可能如下所示:

POST queries/query/_search
{
  "query": {
    "match": {
      "text": "take my order please along with the credit card details"
    }
  },
  "aggs": {
    "src types": {
      "terms": {
        "field": "src"
      },
      "aggs": {
        "best hit": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

对文本查询的搜索限制了聚合的文档集。 "src types"聚合对匹配文档中找到的所有src值进行聚合,"best hit"在每个桶中选择一个最相关的文档(size参数可以根据需要更改)。

查询结果如下:

{
  "hits": {
    "total": 3,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "queries",
        "_type": "query",
        "_id": "VD7QVmABl04oXt2HGbGB",
        "_score": 1.3862944,
        "_source": {
          "text": "Do you accept credit card ?",
          "src": "payment"
        }
      },
      {
        "_index": "queries",
        "_type": "query",
        "_id": "Uj7PVmABl04oXt2HlLFI",
        "_score": 0.8630463,
        "_source": {
          "text": "Take my order",
          "src": "place_order"
        }
      },
      {
        "_index": "queries",
        "_type": "query",
        "_id": "UT7PVmABl04oXt2HKLFy",
        "_score": 0.6931472,
        "_source": {
          "text": "I want to order food",
          "src": "place_order"
        }
      }
    ]
  },
  "aggregations": {
    "src types": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "place_order",
          "doc_count": 2,
          "best hit": {
            "hits": {
              "total": 2,
              "max_score": 0.8630463,
              "hits": [
                {
                  "_index": "queries",
                  "_type": "query",
                  "_id": "Uj7PVmABl04oXt2HlLFI",
                  "_score": 0.8630463,
                  "_source": {
                    "text": "Take my order",
                    "src": "place_order"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "payment",
          "doc_count": 1,
          "best hit": {
            "hits": {
              "total": 1,
              "max_score": 1.3862944,
              "hits": [
                {
                  "_index": "queries",
                  "_type": "query",
                  "_id": "VD7QVmABl04oXt2HGbGB",
                  "_score": 1.3862944,
                  "_source": {
                    "text": "Do you accept credit card ?",
                    "src": "payment"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

希望有帮助!

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 2020-02-08
    • 2019-04-05
    • 2014-12-07
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多