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

编程入门 行业动态 更新时间:2024-10-26 20:33:22
本文介绍了弹性搜索在数组字段上自动完成搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理具有数组类型字符串的文档字段上的自动填充建议。我的文件如下:

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

我正在标签字段执行自动填充搜索。我的查询就像;

I am performing autocomplete search on tags field. My query is like;

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

当用户键入单词我想显示wordland和wordpress。但是,我无法做到这一点。

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

你可以帮忙吗?

谢谢

推荐答案

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

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

1)创建索引:

curl -XPUT "localhost:9200/test_index/"

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

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

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

更多推荐

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

本文发布于:2023-11-30 00:30:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字段   自动完成   弹性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!