# Best way to index user specific products on store index

**URL:** <https://discuss.elastic.co/t/best-way-to-index-user-specific-products-on-store-index/251364>\
**Category:** Elasticsearch\
**Created:** [October 8, 2020, 3:49am UTC](https://discuss.elastic.co/t/best-way-to-index-user-specific-products-on-store-index/251364 "2020-10-08T03:49:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![roman731](https://avatars.discourse-cdn.com/v4/letter/r/bc8723/32.png) [@roman731](https://discuss.elastic.co/u/roman731)\
**Post date:** [October 8, 2020, 3:49am UTC](https://discuss.elastic.co/t/best-way-to-index-user-specific-products-on-store-index/251364/1 "2020-10-08T03:49:14Z")

</div>

I have an index with ~20000 products for an e-commerce website. I would like to implement a "previously purchased feature" for specific users.. so only specific products will return for specific users in this case... but also with a sort on their most frequently purchased item.

If there was no sorting, I think the best way to do this would be just to a store a keyword array of user\_ids on each product.

So my products would look something like:

```auto
{
       "name": "Product A",
       .......
       "bought_by_user_ids": [1,2,3,4,5,8,10]
}

```

And then my query would be something like

```auto
{
"query": {
    "bool": {
      "must": [
        { "match": { "name" : "Product" } }
      ],
      "filter: [
        { "match": { "bought_by_user_ids": 1 } }
      ]
      }
  }
}
Where 1 is the current user's integer ID.

```

The kicker is I also want to sort on most commonly purchased by each user. So my thinking was instead of just keeping user\_ids on the product, I can store an **object** field with user\_ids as keys and the number of times they purchased the product as the values.

So the product would look like:

```auto
{
    "name": "Product A",
     .......
     "bought_by_user_ids": {
         "1": 5,
         "2": 10
      }
}

```

Meaning user with ID 1 has bought the product 5 times, and user with ID 2 has bought the product 10 times.

And the query would be an exists query.

```auto
{
     "query": {
           "exists": {
	            "field": "bought_by_user_ids.1"
	    }
	  },
	  "sort" : [{
             "bought_by_user_ids.1" : {
                  "order" : "desc",
                  "unmapped_type" : "float"
             }
       }]
}
Where 1 is the current user's integer ID.

```

The solution works but I'm wondering if there's a better way to do this? I noticed I may eventually run into a **total\_fields** limit because apparently every entry in the object is its own field? Any help is appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 5, 2020, 3:49am UTC](https://discuss.elastic.co/t/best-way-to-index-user-specific-products-on-store-index/251364/2 "2020-11-05T03:49:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
