栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Spring-Data-Elasticsearch@4.3.1 集成Spring-Boot 使用NativeSearchQueryBuilder构建查询语句查询

Spring-Data-Elasticsearch@4.3.1 集成Spring-Boot 使用NativeSearchQueryBuilder构建查询语句查询

最近有个需求,项目从之前的Spring-Data-Jest换成Spirng-Data-Elasticsearch,Client也换成了最新的7.16.2,这边query这边就需要微改一下。记录下

一开始想用Entity repository的方式,但是发现太麻烦了,需要建立过多的实体,而且query语句也需要大改,后面查看官方文档,看到了使用 org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate的查询方式。

https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.misc.filter 

 pom

    org.springframework.boot
    spring-boot-starter-data-elasticsearch
configuration

这边我是用的是jhipster生成的,所以直接贴出来默认的configuration

package com.damien.test.generated.config;

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 {

        @Override
        public String convert(ZonedDateTime source) {
            if (source == null) {
                return null;
            }
            return source.toInstant().toString();
        }
    }

    @ReadingConverter
    static class ZonedDateTimeReadingConverter implements Converter {

        @Override
        public ZonedDateTime convert(String source) {
            if (source == null) {
                return null;
            }
            return Instant.parse(source).atZone(ZoneId.systemDefault());
        }
    }

    @WritingConverter
    static class InstantWritingConverter implements Converter {

        @Override
        public String convert(Instant source) {
            if (source == null) {
                return null;
            }
            return source.toString();
        }
    }

    @ReadingConverter
    static class InstantReadingConverter implements Converter {

        @Override
        public Instant convert(String source) {
            if (source == null) {
                return null;
            }
            return Instant.parse(source);
        }
    }

    @WritingConverter
    static class LocalDateWritingConverter implements Converter {

        @Override
        public String convert(LocalDate source) {
            if (source == null) {
                return null;
            }
            return source.toString();
        }
    }

    @ReadingConverter
    static class LocalDateReadingConverter implements Converter {

        @Override
        public LocalDate convert(String source) {
            if (source == null) {
                return null;
            }
            return LocalDate.parse(source);
        }
    }
}
yml 
spring:
    elasticsearch:
        rest:
            uris:http://localhost:9200
service 
//import org.springframework.data.domain.Pageable;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import org.springframework.data.elasticsearch.core.SearchHit;
//import org.springframework.data.elasticsearch.core.SearchHits;
//import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.data.elasticsearch.core.query.Query;
//import org.elasticsearch.index.query.TermQueryBuilder;
IndexCoordinates index = IndexCoordinates.of("some_index");

Query query  = new NativeSearchQueryBuilder()
        .withPageable(Pageable.unpaged())
        .withFilter(new TermQueryBuilder("column.keyword", "xxxxx"))
        .build();

SearchHits hit=template.search(query, HashMap.class, index);
for (SearchHit result : hit.getSearchHits()) {
    String somePropertie=(String)result.getContent().get("somePropertie");
     //.....
}

这样就可以构建NativeSearchQuery进行查询了。

当然你要使用repository的方式建立实体可以。 

就类似于JPA的查询方式,建立实体,通过repository进行查询就可以了,就不多赘述了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号