Implementing Suggestions 'xxx in Category' using elasticsearch

限于喜欢 提交于 2019-12-05 16:16:56

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!