Elasticsearch在数组字段上自动完成搜索

 重新入梦 发布于 2023-02-08 07:54

我正在处理具有类型字符串数组的文档字段的自动完成建议.我的文件如下;

{

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

当用户输入"word"时,我想显示"wordland"和"wordpress".但是,我无法做到这一点.

你能帮忙吗?

谢谢

1 个回答
  • 你试过完成建议吗?解决问题的一种方法如下:

    1)创建索引:

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

    2)使用完成建议器类型创建映射:

    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)添加文件:

    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端点进行查询:

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

    然后你会得到你期望的结果:

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

    当然,这种解决方案可以稍微改进一些,特别是通过修剪一些重复的数据,但这应该指向正确的方向.

    2023-02-08 08:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有