0

问题:

我只希望返回每个变体组中的 1 个项目。

背景:我使用的是 Elasticsearch 7。如果我搜索 Bell peppers,应该只列出一项。从技术上讲,我卖 4 个传统甜椒和 4 个有机甜椒。在 Bell Pepper 详细信息页面上,它允许您选择颜色,因此没有理由让搜索结果页面充斥着大量的甜椒。我很确定聚合是解决方案,但是我发现的所有示例以及如何执行此操作的建议对我来说似乎都失败了。

有没有人看到我的错误,或者有一个优雅的方式来实现这一点的建议?

文件结构:

{
  '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
    }
  ]
}
4

1 回答 1

2
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
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

于 2020-04-25T00:51:29.577 回答