问题:

我只希望从每个变体组中退回一件物品。

内容:
我正在使用Elasticsearch 7。
如果我搜索甜椒,应该只列出一项。从技术上讲,我卖4种传统甜椒和4种有机甜椒。在“甜椒”详细信息页面上,您可以选择颜色,因此没有理由在大量的甜椒中填充搜索结果页面。我很确定聚合是解决方案,但是我发现的所有示例以及有关如何执行此操作的建议对我来说都是失败的。

是否有人看到我的错误,或有建议以优雅的方式实现这一目标?

文档结构:

{
  'id': 2843,
  'name': 'Yellow Bell pepper',
  'variant_id': 3311
}

使用的查询:
{
  'query': {
    'query_string': {
      'query': 'bell pepper',
      'fields': [ 'name' ]
    }
  },
  'aggs': {
    'variants' : {
      'terms' : { 'field' : 'variant_id' }
    }
  }
};

当前回复:
  "total": 23,
  "statusCode": 200,
  "hits": [
    {
      "id": 2843,
      "name": "Yellow Bell pepper",
      "variant_id": 3311
    },
    {
      "id": 2842,
      "name": "Orange Bell Pepper",
      "variant_id": 3311
    },
    {
      "id": 2839,
      "name": "Organic Green Bell Pepper",
      "variant_id": 3312
    },
    {
      "id": 2840,
      "name": "Organic Yellow Bell Pepper",
      "variant_id": 3312
    },
  ]
}

我需要的回复:
  "total": 23,
  "statusCode": 200,
  "hits": [
    {
      "id": 2843,
      "name": "Yellow Bell pepper",
      "variant_id": 3311
    },
    {
      "id": 2839,
      "name": "Organic Green Bell Pepper",
      "variant_id": 3312
    }
  ]
}

最佳答案

GET stackoverflow/_search
{
  "query": {
    "query_string": {
      "query": "bell pepper",
      "fields": [
        "name"
      ]
    }
  },
  "aggs": {
    "variants": {
      "terms": {
        "field": "variant_id"
      },
      "aggs": {
        "results": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

结果
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 0.22380026,
    "hits" : [
      {
        "_index" : "stackoverflow",
        "_type" : "_doc",
        "_id" : "Wt7GrnEBo-Xqbvtw2rIB",
        "_score" : 0.22380026,
        "_source" : {
          "id" : 2843,
          "name" : "Yellow Bell pepper",
          "variant_id" : 3311
        }
      },
      {
        "_index" : "stackoverflow",
        "_type" : "_doc",
        "_id" : "W97GrnEBo-Xqbvtw9rKy",
        "_score" : 0.22380026,
        "_source" : {
          "id" : 2842,
          "name" : "Orange Bell Pepper",
          "variant_id" : 3311
        }
      },
      {
        "_index" : "stackoverflow",
        "_type" : "_doc",
        "_id" : "XN7HrnEBo-XqbvtwDbJ2",
        "_score" : 0.19908613,
        "_source" : {
          "id" : 2839,
          "name" : "Organic Green Bell Pepper",
          "variant_id" : 3312
        }
      },
      {
        "_index" : "stackoverflow",
        "_type" : "_doc",
        "_id" : "Xd7HrnEBo-XqbvtwMrLw",
        "_score" : 0.19908613,
        "_source" : {
          "id" : 2840,
          "name" : "Organic Yellow Bell Pepper",
          "variant_id" : 3312
        }
      }
    ]
  },
  "aggregations" : {
    "variants" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 3311,
          "doc_count" : 2,
          "results" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 0.22380026,
              "hits" : [
                {
                  "_index" : "stackoverflow",
                  "_type" : "_doc",
                  "_id" : "Wt7GrnEBo-Xqbvtw2rIB",
                  "_score" : 0.22380026,
                  "_source" : {
                    "id" : 2843,
                    "name" : "Yellow Bell pepper",
                    "variant_id" : 3311
                  }
                }
              ]
            }
          }
        },
        {
          "key" : 3312,
          "doc_count" : 2,
          "results" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 0.19908613,
              "hits" : [
                {
                  "_index" : "stackoverflow",
                  "_type" : "_doc",
                  "_id" : "XN7HrnEBo-XqbvtwDbJ2",
                  "_score" : 0.19908613,
                  "_source" : {
                    "id" : 2839,
                    "name" : "Organic Green Bell Pepper",
                    "variant_id" : 3312
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

这使用filter_path来消除噪音,并将大小设置为0,以不返回任何匹配,因为您将不使用它们。
GET stackoverflow/_search?filter_path=aggregations.variants.buckets.results.hits.hits._source
{
  "size": 0,
  "query": {
    "query_string": {
      "query": "bell pepper",
      "fields": [
        "name"
      ]
    }
  },
  "aggs": {
    "variants": {
      "terms": {
        "field": "variant_id"
      },
      "aggs": {
        "results": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

结果
{
  "aggregations" : {
    "variants" : {
      "buckets" : [
        {
          "results" : {
            "hits" : {
              "hits" : [
                {
                  "_source" : {
                    "id" : 2843,
                    "name" : "Yellow Bell pepper",
                    "variant_id" : 3311
                  }
                }
              ]
            }
          }
        },
        {
          "results" : {
            "hits" : {
              "hits" : [
                {
                  "_source" : {
                    "id" : 2839,
                    "name" : "Organic Green Bell Pepper",
                    "variant_id" : 3312
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

09-15 16:32