环境:Elasticsearch 版本 7.16.3
Elasticsearch for windows下载地址
windows
若依
spring boot版本 2.6.0
pom文件添加
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>8.12.0</version></dependency>
自定义配置类
import org.apache.http.HttpHost;import org.apache.http.client.config.RequestConfig;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.springframework.context.annotation.Configuration;import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;@Configurationpublic class ESConfig extends AbstractElasticsearchConfiguration {@Overridepublic RestHighLevelClient elasticsearchClient() {// 设置连接超时和读取超时时间(单位:毫秒)int connectionTimeout = 60000; // 60 秒int readTimeout = 60000; // 60 秒// 创建 RequestConfigRequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(readTimeout).build();// 创建 RestClientBuilderRestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(httpClientBuilder ->httpClientBuilder.setDefaultRequestConfig(requestConfig));// 创建 RestHighLevelClientRestHighLevelClient client = new RestHighLevelClient(builder);return client;}}
实体类
indexName = “zbinfo” //索引名
import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;@Document(indexName = "zbinfo",shards = 1,replicas = 0)@Data@AllArgsConstructor@NoArgsConstructorpublic class ESZbInfo {@Id@Field(type = FieldType.Keyword)private Long id;@Field(type = FieldType.Long)private Long nid;/** 标题 */@Field(type = FieldType.Text)private String title;@Field(type = FieldType.Text)private String content;}
创建操作的Repository
import com.dataDemo.system.domain.ESZbInfo;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import org.springframework.stereotype.Repository;@Repositorypublic interface ESZbInfoRepstitory extends ElasticsearchRepository<ESZbInfo, String> {}
操作CRUD
@Autowiredprivate ESZbInfoRepstitory esZbInfoRepstitory; @Autowiredprivate ElasticsearchOperations elasticsearchOperations;//批量插入public void insertEsZbInfo(){List<ZbInfoTxy> list= zbInfoTxyService.selectZbInfoTxyListLocal(new ZbInfoTxy());Integer count = 0;List<ESZbInfo> lists = new ArrayList();if(list.size() != 0){for(ZbInfoTxy zbInfoTxy:list){System.out.println("数量----"+count+"-----date---"+ DateUtils.getTime());ESZbInfo zbInfo = new ESZbInfo();zbInfo.setId(zbInfoTxy.getNid());zbInfo.setTitle(zbInfoTxy.getTitle()); zbInfo.setContent(zbInfoTxy.getContent());lists.add(zbInfo);count++;}}//批量插入方法esZbInfoRepstitory.saveAll(lists);}//单个插入 esZbInfoRepstitory.save(new ZbInfoTxy ());//删除public void deleteEsAll(){System.out.println("删除全部:");esZbInfoRepstitory.deleteAll();}//查询 --根据关键词查询public void selectEsZbInfo(String title){System.out.println("根据关键词查询:");/** * //单条件模糊查询 * NativeSearchQuery query = new NativeSearchQueryBuilder() * .withQuery(QueryBuilders.matchPhraseQuery("title", title)) * .build(); * * //多条件匹配查询 *NativeSearchQuery query = new NativeSearchQueryBuilder() * .withQuery(QueryBuilders.boolQuery() * .must(QueryBuilders.matchPhraseQuery("title", title)) * .must(QueryBuilders.matchQuery("city", 98))) * .build(); * * //检索一个字段在两个字段中出现过的 * NativeSearchQuery query = new NativeSearchQueryBuilder() * .withQuery(QueryBuilders.boolQuery() * .must(QueryBuilders.multiMatchQuery(title, "title", "content"))) * .build(); */NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("title", title)).must(QueryBuilders.matchQuery("city", 98))).build();SearchHits<ESZbInfo> searchHits = elasticsearchOperations.search(query, ESZbInfo.class);List<ESZbInfo> list = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());if(list.size() != 0){for(ESZbInfo esZbInfo:list){System.out.println(esZbInfo);}}}//根据id查询public void selectEsById(String id){Optional<ESZbInfo> optionalById = this.esZbInfoRepstitory.findById(id);System.out.println("单个查询---"+optionalById);}
//查询es状态
http://localhost:9200/_cluster/health
//查询索引的zbinfo映射字段
http://localhost:9200/zbinfo/_mapping
如果插入es的数据量过大,可调整Elasticsearch 目录下/config/elasticsearch.yml 配置文件
添加 http.max_content_length: 200mb(可根据需求更改)
然后重启es