4

我目前有以下 POJO。

@Document(indexName="ws",type="vid")
public class Vid {
    @Id 
    private String id;

    @Field(type=FieldType.String, index=FieldIndex.not_analyzed)
    private List<String> tags;
}

表示此 POJO 的 JSON 如下。

{ 
    "id" : "someId",
    "tags" : [ "one", "two", "three" ]
}

我想要的是定义该tags字段的映射,以便我可以在自动完成搜索框中使用这些值。这得到了 Elasticsearch 的 Completion Suggester 的支持。https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html上的文档似乎建议我必须按如下方式设置映射。

{
    "vid": {
        "properties": {
            "id": {
                "type": "string"
            },
            "tags": {
                "type": "completion",
                "index_analyzer": "simple",
                "search_analyzer": "simple",
                "payloads": true
            }
        }
    }
}

但是,这意味着我将不得不修改我的 POJO 和 JSON 表示。

{
    "id": "someId",
    "tags": {
        "input": [ "one", "two", "three" ]
    }
}

Completions Suggesters我发现另一个很好的页面在这里谈论http://blog.qbox.io/quick-and-dirty-autocomplete-with-elasticsearch-completion-suggest。但是,该页面似乎暗示与tags.

{
    "id": "someId",
    "tags": [ "one", "two", "three" ],
    "tags_suggest": {
        "input": [ "one", "two", "three" ]
    }
}

最后,我在http://docs.spring.io/spring-data/elasticsearch/docs/current/api/index.html?org/springframework/data/elasticsearch/core/找到了 spring-data-elasticsearch 的这个 javadoc 页面完成/完成.html。我确信这门课有关系,Completion Suggesters但我不知道如何使用它。

有什么方法可以只使用 Spring 注释来定义 Elasticsearch 映射Completion Suggester吗?

4

2 回答 2

7

绝对没错..

您可以像这样配置您的实体:

...
import org.springframework.data.elasticsearch.core.completion.Completion;
...

@Document(indexName = "test-completion-index", type = "annotated-completion-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
public class YoutEntity {

    @Id
    private String id;
    private String name;

    @CompletionField(payloads = true, maxInputLength = 100)
    private Completion suggest;

    ...
}

例如,检查此链接

于 2015-11-09T16:09:53.030 回答
1

我对此没有经验,但也许这个注释对你有帮助:
链接到 Spring Data Elasticsearch 文档

于 2015-08-24T17:07:14.047 回答