栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elasticsearch对数组字段的自动完成搜索

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch对数组字段的自动完成搜索

您是否尝试过完成建议?解决问题的一种方法如下:

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

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号