以下是示例上下文:
curl -XPUT localhost:9200/testso -d '{
"mappings": {
"person": {
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
curl -XPUT 'http://localhost:9200/testso/person/1' -d '{
"name" : "foo bar",
"context_id" : 1
}'
curl -XPUT 'http://localhost:9200/testso/person/2' -d '{
"name" : "fou bar",
"context_id" : 1
}'
curl -XPUT 'http://localhost:9200/testso/person/3' -d '{
"name" : "foo baz",
"context_id" : 2
}'
我正在寻找一种方法来帮助用户纠正"它在特定的背景下编写并自动完成它。
例子,寻找上下文1:
"foo bar" => [ "foo bar", "fou bar" ]
"fou bar" => [ "foo bar", "fou bar" ]
"foo" => [ "foo", "fou", "foo bar", "fou bar" ]
"fol" => [ "foo", "fou", "foo bar", "fou bar" ]
我测试了一些弹性搜索功能... suggest
没有考虑"query"
选项:
curl -XGET http://localhost:9200/testso/person/_search?search_type=count -d '{
"query" : { "query_string": { "query": "context_id:2" } },
"suggest": {
"text" : "foo baz",
"my_suggestion": {
"term" : {
"field" : "name"
}
}
}
}'
...返回建议" foo bar"和" fou bar",属于上下文1。
您是否了解查询弹性搜索如何返回此类内容?具体的映射?另一个功能?一个特定的索引器?
答案 0 :(得分:0)
不考虑查询选项,因为Elasticsearch使用FST来启用前缀完成功能。它是一种非常快速的内存数据结构,可在1 ms内完成建议。如果要添加查询或过滤器选项,则会减慢建议过程的速度。
关于您的使用案例:他们在1.2.0版本的API文档中宣告context suggester,这应该可以满足您的需求。
与此同时,您可以实施一些步骤:
将上下文信息添加到建议字段以及查询中: 映射
PUT /testo/person/_mapping{
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"name_suggestion": {
"type": "completion"
}
}
}
定义可能的建议点击和输出
PUT /testo/person/1
{
"name": "foo bar",
"name_suggestion": {
"input": [
"onefoo bar",
"onefoo",
"onebar"
], "output": "foo bar"
}
}
PUT /testo/person/2
{
"name": "fou bar",
"name_suggestion": {
"input": [
"onefou bar",
"onefou",
"onebar"
], "output": "fou bar"
}
}
PUT /testo/person/3
{
"name": "foo baz",
"name_suggestion": {
"input": [
"twofou baz",
"twofou",
"twobaz"
], "output": "foo baz"
}
}
POST /testo/_suggest
{
"suggest": {
"text": "twofoo",
"completion": {
"field": "name_suggestion"
}
}
}
请注意,完成建议器使用简单分析器