Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Spring's annotations to define Completion Suggester for a mapping in Elasticsearch?

I currently have the following 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;
}

A JSON that represents this POJO is as follows.

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

What I want is to define the mapping for the tags field so that I can use the values in an auto-complete search box. This is supported by Elasticsearch's Completion Suggester. The documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html seem to suggest to me that I have to set up the mapping as follows.

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

However, that would mean that I would have to revise my POJO and JSON representation.

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

I found another good page talking about Completions Suggesters here http://blog.qbox.io/quick-and-dirty-autocomplete-with-elasticsearch-completion-suggest. However, that page seem to suggest redundancy with the tags.

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

Lastly, I found this javadoc page from spring-data-elasticsearch at http://docs.spring.io/spring-data/elasticsearch/docs/current/api/index.html?org/springframework/data/elasticsearch/core/completion/Completion.html. I am sure this class has something to do with Completion Suggesters but I don't know how to use it.

Is there any way I can just use Spring annotations to define the Elasticsearch mapping for Completion Suggester?

like image 339
Jane Wayne Avatar asked Feb 09 '23 06:02

Jane Wayne


2 Answers

Absolutely yes..

you can configure your entity like this:

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

    ...
}

Check this link for example.

like image 56
Efriandika Pratama Avatar answered Feb 12 '23 11:02

Efriandika Pratama


I am not experienced with that, but maybe this annotation can be helpful for you:
Link to Spring Data Elasticsearch documentation

like image 24
Bartosz Magryś Avatar answered Feb 12 '23 09:02

Bartosz Magryś