Best practice to search for a list of subset holders that holds a subset of a complete set?

29 Views Asked by At

What is the best practice to search a list of stores that holds a subset of goods of a library of goods?

Here is the scenario:

  1. a library of goods has (0 to totalAmountofGoods), each store can hold a subset of the library of goods.
  2. a customer may purchase a list of goods where it is a subset of the library of goods.
  3. what is the best pratice to find out the list of stores can provide all items in customer's shopping list? Assume that we can use hash tables, ES or anything else.
1

There are 1 best solutions below

0
glenacota On

One way to do it in Elasticsearch is to have an index where you have at least the following fields:

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

(mapping)

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

Then, you can create a query searching for as many books as you want, then aggregate the results by store identifiers, and check which store has the highest number of books (by looking at the doc_count). The query would look something like:

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

And the response will be something like:

{
  "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
        }
      ]
    }
  }
}