elasticsearch 搜索包含完整集子集的子集持有者列表的最佳实践是什么?

rdrgkggo  于 2023-03-07  发布在  ElasticSearch
关注(0)|答案(1)|浏览(112)

搜索拥有商品库中商品子集的商店列表的最佳实践是什么?
以下是场景:
1.一个商品库有(0到totalAmountofGoods),每个商店可以保存该商品库的一个子集。
1.顾客可以购买作为商品库的子集的商品列表。
1.什么是最好的实践来找出商店列表可以提供所有的项目在客户的购物清单?假设我们可以使用哈希表,ES或任何其他。

o0lyfsai

o0lyfsai1#

在Elasticsearch中实现这一点的一种方法是创建一个索引,其中至少包含以下字段:

{
  "book": "the book title",
  "store": "the_store_id",
  ...
}

(制图)

{
  "mappings": {
    "properties": {
      "book": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "store": {
        "type": "keyword"
      },
      ...
    }
  }
}

然后,您可以创建一个查询,搜索所需数量的图书,然后按商店标识符聚合结果,并检查哪个商店的图书数量最多(通过查看doc_count)。

GET your_index/_search?filter_path=aggregations
{
  "size": 0,
  "query": {
    "terms": {
      "book.keyword": [
        "title1", "title2", "title3"
      ]
    }
  },
  "aggs": {
    "by_shop": {
      "terms": {
        "field": "store"
      }
    }
  }
}

他们的回答会是这样的:

{
  "aggregations": {
    "by_shop": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "store1",
          "doc_count": 3 <-- the winner
        },
        {
          "key": "store2",
          "doc_count": 2
        },
        {
          "key": "store3",
          "doc_count": 1
        }
      ]
    }
  }
}

相关问题