使用elasticsearch实施建议“类别中的xxx" [英] Implementing Suggestions 'xxx in Category' using elasticsearch

查看:160
本文介绍了使用elasticsearch实施建议“类别中的xxx"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为产品实施类似亚马逊的类别内"建议.亚马逊建议在特定类别中搜索给定术语,而不是全局搜索.这样可以进行更具体的搜索和结果.

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.

有没有办法使用elasticsearch提供的建议功能之一来实现这一目标?

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

当前,我的想法是从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?

推荐答案

如果您事先知道categories,则可以使用

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

我创建了此样本索引

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
  }
}

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

这是我得到的输出

"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.

这符合您的要求吗?

这篇关于使用elasticsearch实施建议“类别中的xxx"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆