这边由于需求原因需要替换,主要记录下Query中的一些写法该怎么改动。
首先是依赖的替换
<dependency>
<groupId>com.github.vanroy</groupId>
<artifactId>spring-boot-starter-data-jest</artifactId>
<version>${spring-data-jest.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
然后是yml
spring:
data:
jest:
uri: http://localhost:9200
spring:
elasticsearch:
rest:
uris: https://localhost:9200
接着是config
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.vanroy.springdata.jest.JestElasticsearchTemplate;
import com.github.vanroy.springdata.jest.mapper.DefaultJestResultsMapper;
import io.searchbox.client.JestClient;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.MappingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfiguration {
private ObjectMapper mapper;
public ElasticsearchConfiguration(ObjectMapper mapper) {
this.mapper = mapper;
}
@Bean
public EntityMapper getEntityMapper() {
return new CustomEntityMapper(mapper);
}
@Bean
@Primary
public JestElasticsearchTemplate elasticsearchTemplate(JestClient jestClient,
ElasticsearchConverter elasticsearchConverter,
SimpleElasticsearchMappingContext mappingContext,
EntityMapper entityMapper) {
return new JestElasticsearchTemplate(
jestClient,
elasticsearchConverter,
new DefaultJestResultsMapper(mappingContext, entityMapper));
}
public class CustomEntityMapper implements EntityMapper {
private ObjectMapper objectMapper;
public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
}
@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
@Override
public Map<String, Object> mapObject(Object source) {
try {
return objectMapper.readValue(mapToString(source), HashMap.class);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
}
}
@Override
public <T> T readObject(Map<String, Object> source, Class<T> targetType) {
try {
return mapToObject(mapToString(source), targetType);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
}
}
}
}
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
@Configuration
public class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
@Bean
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(
new ZonedDateTimeWritingConverter(),
new ZonedDateTimeReadingConverter(),
new InstantWritingConverter(),
new InstantReadingConverter(),
new LocalDateWritingConverter(),
new LocalDateReadingConverter()
)
);
}
@WritingConverter
static class ZonedDateTimeWritingConverter implements Converter<ZonedDateTime, String> {
@Override
public String convert(ZonedDateTime source) {
if (source == null) {
return null;
}
return source.toInstant().toString();
}
}
@ReadingConverter
static class ZonedDateTimeReadingConverter implements Converter<String, ZonedDateTime> {
@Override
public ZonedDateTime convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source).atZone(ZoneId.systemDefault());
}
}
@WritingConverter
static class InstantWritingConverter implements Converter<Instant, String> {
@Override
public String convert(Instant source) {
if (source == null) {
return null;
}
return source.toString();
}
}
@ReadingConverter
static class InstantReadingConverter implements Converter<String, Instant> {
@Override
public Instant convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source);
}
}
@WritingConverter
static class LocalDateWritingConverter implements Converter<LocalDate, String> {
@Override
public String convert(LocalDate source) {
if (source == null) {
return null;
}
return source.toString();
}
}
@ReadingConverter
static class LocalDateReadingConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {
if (source == null) {
return null;
}
return LocalDate.parse(source);
}
}
}
接下来就是重点的Query的改动了。
index
//import org.springframework.data.elasticsearch.core.query.SearchQuery;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder
SearchQuery sq = new NativeSearchQueryBuilder()
.withIndices(esIndex)
.withTypes("doc")
.withQuery(new TermQueryBuilder("_id", id))
.withPageable(Pageable.unpaged())
.build();
HashMap result = esTemplate.query(sq, new JestResultsExtractor<ESResult>() {
@Override
public HashMap extract(SearchResult response) {
//..............
//return esResult;
}
});
现在index是需要单独配置了
//import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
//import org.springframework.data.elasticsearch.core.query.Query;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.data.elasticsearch.core.SearchHits;
IndexCoordinates esindex = IndexCoordinates.of(esIndex);
Query sq = new NativeSearchQueryBuilder().withQuery(new TermQueryBuilder("_id", id))
.withPageable(Pageable.unpaged())
.build();
SearchHits<HashMap> hits = template.search(sq, HashMap.class, esindex);
接下来就是重点了中的重点了,之前的自定义返回值的各种提取方式。
Hit提取
//com.google.gson.JsonArray
esTemplate.query(sq, new JestResultsExtractor<ESResult>() {
@Override
public ESResult extract(SearchResult response) {
JsonArray array=response.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
//return ....;
}
});
//org.springframework.data.elasticsearch.core.SearchHits
//org.springframework.data.elasticsearch.core.SearchHit
//....
SearchHits<HashMap> hits = template.search(sq, HashMap.class, esindex);
List<SearchHit<HashMap>> hit = hits.getSearchHits();
Filter提取
我就不写之前的代码了 变量都是一样的对照一下就知道变量是怎么来的了。
FilterAggregation ft=response.getAggregations().getFilterAggregation("service");
//org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter
SearchHits<HashMap> hits = template.search(sq, HashMap.class, esindex);
ParsedFilter ft = hits.getAggregations().get("service");
像这种连续获取的,就需要强转一下了
FilterAggregation ft =response.getAggregations().getFilterAggregation("service").getFilterAggregation("sla");
ParsedFilter ft=((ParsedFilter) ft.getAggregations().get("service")).getAggregations().get("sla");
Filters提取
和filter类似
//io.searchbox.core.search.aggregation.Bucket
for (Bucket bucket : response.getAggregations().getFilterAggregation("filter").getFiltersAggregation("time").getBuckets()) {
//...
}
//org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter
//org.elasticsearch.search.aggregations.bucket.filter.ParsedFilters
//org.elasticsearch.search.aggregations.bucket.filter.Filters.Bucket
for (Bucket bucket : ((ParsedFilters)((ParsedFilter)hits.getAggregations().get("filter"))
.getAggregations().get("time")).getBuckets()) {
//...
}
Terms提取
//io.searchbox.core.search.aggregation.TermsAggregation.Entry
for (Entry entry: response.getAggregations().getTermsAggregation("status").getBuckets()) {
map.put(entry.getKey(), entry.getCount());
}
//org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket
//org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms
for (Bucket innBucket : ((ParsedStringTerms) hits.getAggregations().get("status")).getBuckets()) {
map.put(innBucket.getKeyAsString(), innBucket.getDocCount());
}
TopHits提取
TopHitsAggregation top=entry.getTopHitsAggregation("top");
//org.elasticsearch.search.aggregations.metrics.TopHits
TopHits topScoreResult = bucket.getAggregations().get("top");
for (Entry entry : response.getAggregations().getTermsAggregation("host").getBuckets()) {
for (JsonElement hit: entry.getTopHitsAggregation("top").getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray()) {
map.put("id",hit.getAsJsonObject().get("_id").getAsString());
map.put("source",new ObjectMapper().readValue(String.valueOf(hit.getAsJsonObject().get("_source")), HashMap.class));
}
}
ParsedStringTerms terms = hits.getAggregations().get("host");
for (Bucket bucket : terms.getBuckets()) {
TopHits topScoreResult = bucket.getAggregations().get("top");
for (SearchHit hit : topScoreResult.getHits().getHits()) {
map.put("id",hit.getId());
map.put("source",hit.getSourceAsMap());
}
}
还有就是类似 max min 或者avg的,大致就是parsed+需要的类型,例如max就是parsedMax。
//org.elasticsearch.search.aggregations.metrics.ParsedMax
ParsedMax max=innBucket.getAggregations().get("max");
//max.getValue();
org.elasticsearch.search.aggregations.metrics.ParsedMin
ParsedMin min=innBucket.getAggregations().get("min");
//min.getValue();
//org.elasticsearch.search.aggregations.metrics.ParsedAvg
ParsedAvg avg = bucket.getAggregations().get("average");
//avg.getValue();
大致现在差不多就遇到这么多问题了。
|