I'm already able to get suggestions by using cURL (see code blocks). But I need to do this in my code so I can use my custom endpoint TOMCAT_ROOT/suggest?q=el. How can/should I create a query, using Spring Data, to get the same result in Java code.
My es mapping:
{
"textvalue": {
"properties": {
"id": {
"type": "string",
"index": "not_analyzed"
},
"fieldId": {
"type": "string",
"index": "not_analyzed"
},
"code": {
"type": "string",
"index": "not_analyzed"
},
"translations": {
"type": "nested",
"index": "not_analyzed"
},
"createdOn": {
"type": "date",
"format": "date_hour_minute_second"
},
"lastUpdatedOn": {
"type": "date",
"format": "date_hour_minute_second"
},
"suggest": {
"type": "completion",
"index_analyzer": "simple",
"search_analyzer": "simple",
"payloads": false
}
}
}
}
My POJO:
package be.smartask.service.data;
import be.smartsearch.service.Language;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Mapping;
import java.util.Date;
import java.util.Map;
/**
* @author Glenn Van Schil
* Created on 26/01/2016
*/
@Document(indexName = "smartask", type = "textvalue")
@Mapping(mappingPath = "/es-mapping/textvalue-mapping.json")
public class TextValue extends Value {
private String code;
private Map<Language, String> translations;
private Suggest suggest;
public TextValue() {
}
public TextValue(String id, String fieldId, Date createdOn, Date lastUpdatedOn, String code, Map<Language, String> translations, Suggest suggest) {
super(id, fieldId, createdOn, lastUpdatedOn);
this.code = code;
this.translations = translations;
this.suggest = suggest;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Map<Language, String> getTranslations() {
return translations;
}
public void setTranslations(Map<Language, String> translations) {
this.translations = translations;
}
public Suggest getSuggest() {
return suggest;
}
public void setSuggest(Suggest suggest) {
this.suggest = suggest;
}
}
My es suggest query:
{
"suggestions": {
"text": "el",
"completion": {
"field": "suggest",
"size": "10",
"fuzzy": {
"fuzziness": "AUTO"
}
}
}
}
My query result:
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"suggestions": [{
"text": "el",
"offset": 0,
"length": 2,
"options": [{
"text": "Electrabel",
"score": 1.0
}, {
"text": "Elision",
"score": 1.0
}]
}]
}