赞
踩
注:ES和Maven引用的版本尽量一致
使用Maven的情况下
- <dependency>
- <groupId> org.elasticsearch.client </groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- <version>7.12.0</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>7.12.0</version>
- </dependency>
这两个就够用了
如果没使用Maven项目管理集成Es的话
https://download.csdn.net/download/gaolengroujiamo/16245088
在上面这个路径去下载集成Jar包去,已经给大家准备好了。
分隔线================================================================================================================================
ElasticSearch准备的基础数据
代码示例
- package com.example.demoes.controller;
-
- import org.apache.http.HttpHost;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestHighLevelClient;
-
- import java.io.IOException;
-
- public class EsApiController {
- public static org.elasticsearch.client.RestHighLevelClient RestHighLevelClient= new RestHighLevelClient(
- RestClient.builder(
- new HttpHost("localhost", 9200, "http"),
- new HttpHost("localhost", 9201, "http")));//初始化
- private static IndexApi indexApi = new IndexApi();
- private static SelectApi searchApi = new SelectApi();
- public static void main(String[] args) throws IOException {
- //indexApi.CreateIndexApi();//创建索引
- //indexApi.QueryIndexApi();//查询索引是否存在
- //indexApi.GetIndexDocumet("blog","2");
- //searchApi.SearchAll();
- //searchApi.SearchSoleParam("么稳");
- searchApi.SearchMuchParam("这是一段话");
- //searchApi.SearchParam("A350-A-71-00-51-00001-720A-A");
- //searchApi.SearchParam("JCNUMBER");
-
- EsApiController.close();
- }
- public static void close() throws IOException {
- RestHighLevelClient.close();
- }
- }
索引API
- package com.example.demoes.controller;
-
- import org.elasticsearch.action.get.GetRequest;
- import org.elasticsearch.action.get.GetResponse;
- import org.elasticsearch.action.index.IndexRequest;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.elasticsearch.client.indices.CreateIndexRequest;
- import org.elasticsearch.client.indices.CreateIndexResponse;
- import org.elasticsearch.client.indices.GetIndexRequest;
- import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.io.IOException;
- import java.util.Map;
-
- public class IndexApi {
- private static Logger logger = LoggerFactory.getLogger(IndexApi.class);
- private static RestHighLevelClient restHighLevelClient =EsApiController.RestHighLevelClient;
- /**
- * 创建索引
- */
- public void CreateIndexApi() throws IOException {
- CreateIndexRequest indexRequest = new CreateIndexRequest("addindex");
- CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(indexRequest, RequestOptions.DEFAULT);
- logger.info(createIndexResponse.toString());
- System.out.println(createIndexResponse);
- }
- /**
- * 查找索引
- */
- public void QueryIndexApi() throws IOException {
- GetIndexRequest indexRequest = new GetIndexRequest("addindex");
- boolean createIndexResponse = restHighLevelClient.indices().exists(indexRequest,RequestOptions.DEFAULT);
- logger.info(createIndexResponse+"");
- System.out.println(createIndexResponse);
- }
- /**
- * 查找索引
- */
- public void GetIndexDocumet(String indexName,String id ) throws IOException {
- GetRequest indexRequest = new GetRequest(indexName,id);
- indexRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
- GetResponse documentFields = restHighLevelClient.get(indexRequest, RequestOptions.DEFAULT);
- System.out.println(documentFields.getSource().toString());
-
- }
- }
简单查询API
- package com.example.demoes.controller;
-
- import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
- import org.elasticsearch.action.get.GetRequest;
- import org.elasticsearch.action.get.GetResponse;
- import org.elasticsearch.action.search.SearchRequest;
- import org.elasticsearch.action.search.SearchResponse;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.elasticsearch.client.indices.CreateIndexRequest;
- import org.elasticsearch.client.indices.CreateIndexResponse;
- import org.elasticsearch.client.indices.GetIndexRequest;
- import org.elasticsearch.common.unit.Fuzziness;
- import org.elasticsearch.index.query.BoolQueryBuilder;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.SearchHit;
- import org.elasticsearch.search.SearchHits;
- import org.elasticsearch.search.aggregations.Aggregation;
- import org.elasticsearch.search.builder.SearchSourceBuilder;
- import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.io.IOException;
- import java.util.List;
-
- public class SelectApi {
- private static Logger logger = LoggerFactory.getLogger(SelectApi.class);
- private static RestHighLevelClient restHighLevelClient =EsApiController.RestHighLevelClient;
- /**
- * 全部查询
- */
- public void SearchAll() throws IOException {
- SearchRequest searchRequest = new SearchRequest();
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.matchAllQuery());
- searchSourceBuilder.size(50);
- searchRequest.source(searchSourceBuilder);
- SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- SearchHits hits = search.getHits();
- for(SearchHit hit : hits){
- String sourceAsString = hit.getSourceAsString();
- if (sourceAsString != null) {
- System.out.println(sourceAsString);
- }
- }
- }
-
- /**
- * 模糊匹配单独字段查询
- * @param param
- * @throws IOException
- */
- public void SearchSoleParam(String param) throws IOException {
- SearchRequest searchRequest = new SearchRequest();
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.matchQuery("title",param).fuzziness(Fuzziness.AUTO));//无效
- searchSourceBuilder.query(QueryBuilders.matchQuery("content",param).fuzziness(Fuzziness.AUTO));//无效
- searchSourceBuilder.query(QueryBuilders.matchQuery("jcXmlContent",param).fuzziness(Fuzziness.AUTO));//无效
- searchSourceBuilder.query(QueryBuilders.matchQuery("jcNumber",param).fuzziness(Fuzziness.AUTO));//无效
- searchSourceBuilder.query(QueryBuilders.matchQuery("author",param).fuzziness(Fuzziness.AUTO));//有效
- searchSourceBuilder.size(50);
- searchRequest.source(searchSourceBuilder);
- SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- SearchHits hits = search.getHits();
- for(SearchHit hit : hits){
- String sourceAsString = hit.getSourceAsString();
- if (sourceAsString != null) {
- System.out.println("参数:"+sourceAsString);
- }
- }
- }
- /**
- * 模糊匹配多字段查询
- * @param param
- * @throws IOException
- */
- public void SearchMuchParam(String param) throws IOException {
- SearchRequest searchRequest = new SearchRequest();
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("title",param));
- boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("content",param));
- boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("jcXmlContent",param));
- boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("jcNumber",param));
- boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("author",param));
- searchSourceBuilder.size(50) ;
- searchSourceBuilder.query(boolQueryBuilder);
- searchRequest.source(searchSourceBuilder);
- SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- SearchHits hits = search.getHits();
- for(SearchHit hit : hits){
- String sourceAsString = hit.getSourceAsString();
- if (sourceAsString != null) {
- System.out.println("参数:"+sourceAsString);
- }
- }
- }
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- }
查询出来的数据
一些复杂的查询可以去ES官网查看一下具体的API
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
如果对您有帮助的话劳烦您的小手点点赞
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。