java - How to use Elasticsearch suggestions with Spring data? -
i'm able suggestions using curl (see code blocks). need in code can use custom endpoint tomcat_root/suggest?q=el. how can/should create query, using spring data, 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 }] }] }
i recommend use store template (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html#_stored_templates), lets store suggestion template in es.
then execute suggestion java code (https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.6/java-search-template.html) referring template name , passing argument.
Comments
Post a Comment