对数组字段Elasticsearch自动完成搜索 [英] Elasticsearch autocomplete search on array field

查看:330
本文介绍了对数组字段Elasticsearch自动完成搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作对文档字段的自动完成建议,具有字符串类型的数组。我的文档是像下面;

I am working on autocomplete suggestion on document field that has array of type string. My document is like below;

{

    "title": "Product1",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "magazine",
        "responsive",
        "two columns",
        "wordpress"
    ],
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}

{

    "title": "Product1",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
    ],
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}

我在标签进行自动完成搜索字段。我查询等;

query: {
                    query_string: {
                        query: "word*",
                        fields: ["tags"]
                    }
                },
                facets: {
                    tags: {
                        terms: {
                            field: "tags"
                        }
                    }
                }

当用户键入词我想显示wordland和字preSS。然而,我无法管理这样做。

When user type "word" I want to display "wordland" and "wordpress". However, I couldn't manage to do that.

能否请你在这方面的帮助?

Could you please help on this?

感谢

推荐答案

你试过<一个href=\"http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html\">completion建议的?为您解决问题的方法之一是:

Have you tried completion suggest? One way to solve your problem is as follows:

1)创建索引:

curl -XPUT "http://localhost:9200/test_index/"

2)创建的映射,使用完成suggester类型:

2) Create the mapping, using the completion suggester type:

curl -XPUT "http://localhost:9200/test_index/product/_mapping" -d'
{
   "product": {
      "properties": {
         "category": {
            "type": "string"
         },
         "cost": {
            "type": "string"
         },
         "createDate": {
            "type": "date",
            "format": "dateOptionalTime"
         },
         "description": {
            "type": "string"
         },
         "rating": {
            "type": "string"
         },
         "sales": {
            "type": "string"
         },
         "tags": {
            "type": "string"
         },
         "title": {
            "type": "string"
         },
         "suggest": {
            "type": "completion",
            "index_analyzer": "simple",
            "search_analyzer": "simple",
            "payloads": false
         }
      }
   }
}'

3)添加您的文件:

3) Add your documents:

curl -XPUT "http://localhost:9200/test_index/product/1" -d'
{
   "title": "Product1",
   "sales": "6",
   "rating": "0.0",
   "cost": "45.00",
   "tags": [
      "blog",
      "magazine",
      "responsive",
      "two columns",
      "wordpress"
   ],
   "suggest": {
      "input": [
         "blog",
         "magazine",
         "responsive",
         "two columns",
         "wordpress"
      ]
   },
   "category": "wordpress",
   "description": "Product1 Description",
   "createDate": "2013-12-19"
}'

curl -XPUT "http://localhost:9200/test_index/product/2" -d'
{

    "title": "Product2",
    "sales": "6",
    "rating": "0.0",
    "cost": "45.00",
    "tags": [
        "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
    ],
   "suggest": {
      "input": [
         "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
      ]
   },
    "category": "wordpress",
    "description": "Product1 Description",
    "createDate": "2013-12-19"
}'

4),然后查询中使用_suggest端点:

4) And then query using the _suggest endpoint:

curl -XPOST "http://localhost:9200/test_index/_suggest" -d'
{
    "product_suggest":{
        "text":"word",
        "completion": {
            "field" : "suggest"
        }
    }
}'

,你会得到结果反馈,你预期:

and you will get the results back that you expected:

{
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "product_suggest": [
      {
         "text": "word",
         "offset": 0,
         "length": 4,
         "options": [
            {
               "text": "wordland",
               "score": 1
            },
            {
               "text": "wordpress",
               "score": 1
            }
         ]
      }
   ]
}

该解决方案可以被提炼了一下,当然,特别是一些修剪重复数据,但这应该指向您在正确的方向。

This solution could be refined a bit, of course, particularly by pruning some duplicate data, but this should point you in the right direction.

这篇关于对数组字段Elasticsearch自动完成搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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