
@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" ]
}
我想要的是定义标签字段的映射,以便我可以在自动完成搜索框中使用这些值.这得到了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" ]
}
}
我在http://blog.qbox.io/quick-and-dirty-autocomplete-with-elasticsearch-completion-suggest发现了另一个关于Completions Suggesters的好页面.然而,该页面似乎暗示了标签的冗余.
{
"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/completion/Completion.html发现了spring-data-elasticsearch的这个javadoc页面.我确信这个类与Completion Suggesters有关,但我不知道如何使用它.
有什么方法可以使用Spring注释来为Completion Suggester定义Elasticsearch映射吗?
您可以像这样配置您的实体:
...
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;
...
}
例如,检查this link.
转载注明原文:java – 是否可以使用Spring的注释为Elasticsearch中的映射定义Completion Suggester? - 乐贴网