我已经能够通过使用cURL获得建议(参见代码块)。但是我需要在我的代码中这样做,这样我就可以使用我的自定义endpointTOMCAT_ROOT/suggest?q=el。如何使用Spring数据创建查询,以在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;
}
}
我的问题是:
{
"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
}]
}]
}
我通过使用Spring的RestTemplate实现了这一点
package be.smartask.service.suggestions;
import be.smartask.service.model.Suggestions;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @author Glenn Van Schil
* Created on 8/02/2016
*/
@Service
public class SuggestionService {
public Suggestions getSuggestions(String word) {
String uri = "http://localhost:9200/smartask/_suggest";
String json = "{\"suggestions\":{\"text\":\"" + word + "\",\"completion\":{\"field\":\"suggest\",\"size\":\"10\",\"fuzzy\":{\"fuzziness\":\"AUTO\"}}}}";
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
RestTemplate restTemplate = new RestTemplate(requestFactory);
Suggestions response = restTemplate.postForObject(uri, json, Suggestions.class);
return response;
}
}
建议对象是基于弹性输出生成的
package be.smartask.service.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"_shards",
"suggestions"
})
public class Suggestions {
/**
*
* (Required)
*
*/
@JsonProperty("_shards")
private be.smartask.service.model.Shards Shards;
/**
*
* (Required)
*
*/
@JsonProperty("suggestions")
private List<Suggestion> suggestions = new ArrayList<Suggestion>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
protected final static Object NOT_FOUND_VALUE = new Object();
/**
*
* (Required)
*
* @return
* The Shards
*/
@JsonProperty("_shards")
public be.smartask.service.model.Shards getShards() {
return Shards;
}
/**
*
* (Required)
*
* @param Shards
* The _shards
*/
@JsonProperty("_shards")
public void setShards(be.smartask.service.model.Shards Shards) {
this.Shards = Shards;
}
/**
*
* (Required)
*
* @return
* The suggestions
*/
@JsonProperty("suggestions")
public List<Suggestion> getSuggestions() {
return suggestions;
}
/**
*
* (Required)
*
* @param suggestions
* The suggestions
*/
@JsonProperty("suggestions")
public void setSuggestions(List<Suggestion> suggestions) {
this.suggestions = suggestions;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@SuppressWarnings({
"unchecked"
})
protected boolean declaredProperty(String name, Object value) {
if ("_shards".equals(name)) {
if (value instanceof be.smartask.service.model.Shards) {
setShards(((be.smartask.service.model.Shards) value));
} else {
throw new IllegalArgumentException(("property \"_shards\" is of type \"be.smartask.service.model.Shards\", but got "+ value.getClass().toString()));
}
return true;
} else {
if ("suggestions".equals(name)) {
if (value instanceof List) {
setSuggestions(((List<Suggestion> ) value));
} else {
throw new IllegalArgumentException(("property \"suggestions\" is of type \"java.util.List<be.smartask.service.model.Suggestion>\", but got "+ value.getClass().toString()));
}
return true;
} else {
return false;
}
}
}
@SuppressWarnings({
"unchecked"
})
protected Object declaredPropertyOrNotFound(String name, Object notFoundValue) {
if ("_shards".equals(name)) {
return getShards();
} else {
if ("suggestions".equals(name)) {
return getSuggestions();
} else {
return notFoundValue;
}
}
}
@SuppressWarnings({
"unchecked"
})
public<T >T get(String name) {
Object value = declaredPropertyOrNotFound(name, Suggestions.NOT_FOUND_VALUE);
if (Suggestions.NOT_FOUND_VALUE!= value) {
return ((T) value);
} else {
return ((T) getAdditionalProperties().get(name));
}
}
@SuppressWarnings({
"unchecked"
})
public void set(String name, Object value) {
if (!declaredProperty(name, value)) {
getAdditionalProperties().put(name, ((Object) value));
}
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(Shards).append(suggestions).append(additionalProperties).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Suggestions) == false) {
return false;
}
Suggestions rhs = ((Suggestions) other);
return new EqualsBuilder().append(Shards, rhs.Shards).append(suggestions, rhs.suggestions).append(additionalProperties, rhs.additionalProperties).isEquals();
}
}
我建议使用商店模板(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)通过引用模板名称并传递参数。
我需要运行以下查询: 但我不能用spring data elasticsearch轻松运行这个。 有什么办法吗 spring data elasticsearch是否很好地支持所有elasticsearch查询DSL
Elasticsearch将删除版本8中的_类型。我正在使用ElasticRepository开发一个Spring boot微服务,所以我想设置实体以避免使用它。据我所知,如果不在@Document中隐式指定类型,它将采用小写的类名。?如何指定不使用类型,而使用type=“\u doc”? 弹性搜索: 7.5.1Spring启动: 2.2.1
升级到Spring boot 2.3和Spring data elasticsearch 4.0.9后的问题。我有这样的文档: 这在spring data 3.0中与Jackson配合得很好,但升级到4.0后,Jackson不再可用,现在我收到了一个来自spring的实例化异常,无法实例化URL对象。 例外情况: 任何关于解决方案的想法都将受到赞赏。
在弹性搜索中,我将记录存储在命名空间和主题中。这些是简单的对象,只有(string)和。 我想通过自动完成搜索名称,为此我试图使用索引搜索。 ElasticSearch::Transport::Transport::Transport::Errors::BadRequest:[400]{“error”:{“root_cause”:[{“type”:“illegal_argument_excepti
我试图用Spring Boot和弹性搜索设置一个应用程序。这个应用程序已经使用Spring Data JPA存储库来持久化我的实体。当我试图在启用弹性搜索配置的情况下运行应用程序时,我遇到的问题是,当存储库被扫描时,我得到了一个异常。 我得到了以下例外: 我的存储库的定义如下: 异常似乎是由于count查询的签名导致的,该签名返回一个int。尽管这个存储库可以很好地处理JPA,但它会抛出一个异常,
我用的是: 这些版本: Spring芯 4.3.7 Spring数据弹性搜索 3.0.9 发布 ES:“版本”:“6.3.0” 组织弹性搜索客户端:rest:5.5.0 和传输 5.5.0 Spring数据共享 1.12.8.发布 创建IAuditElasticRepository.java空接口: 创建了ch-elastic.xml配置: 在我的Manager类中自动安装它: 有一个名为ch-d