首页 > 解决方案 > 在 Elastic Search 中使用某些字段和某些条件进行查询?

问题描述

我有关于产品的数据,其中有一些字段(_id, Shop, ProductVerion...)。它已在 Elastic Search 中编入索引。我想用 Shop 搜索具有最大 ProductVersion 的产品。

前任:

Shop Amazon has 3 Version crawl product: 111,222,333.
Shop Ebay has 2 version: 222,444
Shop Alibaba has 2 version: 111, 444

版本可能相同。

现在,我想获得具有以下功能的产品:

Shop Amazon and ProducVersion 333
or Shop Ebay and ProductVersion 444
or Shop Alibaba and ProductVersion 444.

但我不知道查询。帮帮我,请!

标签: dataframeapache-sparkelasticsearch

解决方案


我用一些示例文档进行了尝试。我将版本字段保留为数字字段。

这些是我尝试过的示例文档

[
  {
    "_index": "test",
    "_type": "doc",
    "_id": "12334",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 341
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "123",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 3412
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1233",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 341
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1238",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 34120
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1239",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 3414
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "123910",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 124
    }
  }
]

正如@demas 指定的那样,我继续进行术语聚合和热门聚合

indexName/_search

{
  "size": 0,
  "aggs": {
    "shop": {
      "terms": {
        "field": "shopName.keyword"
      },
      "aggs": {
        "product": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "version": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

这应该为您提供包含每个商店的最高产品版本号的文档,如下所示。

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "shop": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "alibaba",
          "doc_count": 3,
          "product": {
            "hits": {
              "total": 3,
              "max_score": null,
              "hits": [
                {
                  "_index": "test",
                  "_type": "doc",
                  "_id": "1238",
                  "_score": null,
                  "_source": {
                    "shopName": "alibaba",
                    "version": 34120
                  },
                  "sort": [
                    34120
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "amazon",
          "doc_count": 3,
          "product": {
            "hits": {
              "total": 3,
              "max_score": null,
              "hits": [
                {
                  "_index": "test",
                  "_type": "doc",
                  "_id": "123",
                  "_score": null,
                  "_source": {
                    "shopName": "amazon",
                    "version": 3412
                  },
                  "sort": [
                    3412
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
} 

推荐阅读