6

I want to implement amazon-like 'in-category' suggestions for products. Amazon proposes to search for a given term in a specific category instead of a global search. This allows for a more specific search and results.

Amazon Suggestion 'in-category'

Is there a way how to implement this using one of the suggestion functions provided by elasticsearch?

Currently my idea is to get the suggestions from elasticsearch and group them by category as a post processing. Is there something built in, returning the ready 'in-category' results?

paweloque
  • 18,466
  • 26
  • 80
  • 136
  • will you be knowing `categories` in advance i.e when you index the data meaning when you index say `phones`, will you have knowledge of `Electronics`,`office products` etc? or you would like to query separately for categories? – ChintanShah25 Jan 20 '16 at 14:57
  • Yes, I know the `categories`in advance. – paweloque Jan 20 '16 at 14:59

1 Answers1

3

If you know categories in advance, you could pass them as payload using completion suggester.

I created this sample index

PUT suggest_index/product/_mapping
{
  "product": {
    "properties": {
      "name": {
        "type": "string"
      },
      "suggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple",
        "payloads": true
      }
    }
  }
}

Then I inserted couple of products, supplying categories in payload

PUT suggest_index/product/11
{
  "name": "watches",
  "suggest": {
    "input": [
      "watches"
    ],
    "payload": {
      "categories": [
        "Men",
        "Women",
        "Kids"
      ]
    },
    "weight": 10
  }
}

and

{
  "name": "phones",
  "suggest": {
    "input": [
      "phones"
    ],
    "payload": {
      "categories": [
        "Electronics",
        "Office Products"
      ]
    },
    "weight": 10
  }
}

Then when you query, you will get all categories back with suggestion.

POST suggest_index/_suggest
{
  "product-suggest": {
    "text": "pho",
    "completion": {
      "field": "suggest"
    }
  }
}

This is the output I get

"product-suggest": [
      {
         "text": "pho",
         "offset": 0,
         "length": 3,
         "options": [
            {
               "text": "phones",
               "score": 10,
               "payload": {
                  "categories": [
                     "Electronics",
                     "Office Products"
                  ]
               }
            }
         ]
      }
   ]

Now you can show them in frontend and when user hits search button you could search in possibly different index with that category and product info.

Does this fulfill your requirements?

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • 1
    If you index an additional product with a name `phones mobile`, you'll get both results: `phones` and `phones mobile`. The categories will not be grouped. But maybe I'm looking too far. Maybe a simple aggregation on `categories` would do the job.. – paweloque Jan 20 '16 at 15:32
  • Yes, you are right, but as you said aggregation could help you. – ChintanShah25 Jan 20 '16 at 15:38