首页 文章

是否可以使用Spring的注释为Elasticsearch中的映射定义Completion Suggester?

提问于
浏览
3

我目前有以下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/completion/Completion.html找到了spring-data-elasticsearch的这个javadoc页面 . 我确信这个课与_789117有关,但我不知道如何使用它 .

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

2 回答

  • 5

    绝对没错..

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

    ...
    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 .

  • 1

    我没有这方面的经验,但也许这个注释可能对你有所帮助:
    Link to Spring Data Elasticsearch documentation

相关问题