IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> SpringDataElasticsearch 替换 SpringDataJest -> 正文阅读

[Java知识库]SpringDataElasticsearch 替换 SpringDataJest

这边由于需求原因需要替换,主要记录下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();

大致现在差不多就遇到这么多问题了。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-04-09 18:09:53  更:2022-04-09 18:13:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 23:13:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码