gpt4 book ai didi

java - 如何将 Elasticsearch 建议与 Spring 数据一起使用?

转载 作者:行者123 更新时间:2023-12-04 23:56:31 29 4
gpt4 key购买 nike

我已经能够通过使用 cURL 获得建议(参见代码块)。但我需要在我的代码中执行此操作,以便我可以使用我的自定义端点 TOMCAT_ROOT/suggest?q=el。我如何/应该如何使用 Spring Data 创建查询以在 Java 代码中获得相同的结果。

我的es映射:

{
"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
}
}
}
}

我的 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;
}
}

我的es建议查询:

{
"suggestions": {
"text": "el",
"completion": {
"field": "suggest",
"size": "10",
"fuzzy": {
"fuzziness": "AUTO"
}
}
}
}

我的查询结果:

{
"_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
}]
}]
}

最佳答案

我建议使用 Store Template ( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html#_stored_templates ),它基本上可以让您将建议存储为 ES 中的模板。

然后通过引用模板名称并传递您的参数,从您的 Java 代码 (https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.6/java-search-template.html) 中执行建议。

关于java - 如何将 Elasticsearch 建议与 Spring 数据一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266185/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com