赞
踩
首先引入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.13</version>
</dependency>
这个依赖中已经包含了一下这些依赖,我们只需要在项目中引入这个依赖即可。
配置
es使用的是集群配置。引入上面的依赖后我们只需要在springboot项目当中添加以下的配置即可。
spring:
data:
elasticsearch:
repositories:
enabled: true
elasticsearch:
username: your username
password: your password word
uris: http://ip1:port1,http://ip2:port2,http://ip3:port3
使用java操作es
首先引入bean。注意后面导包的时候要用org下面的包而不是co下面的。
import org.elasticsearch.client.RestHighLevelClient;
@Autowired
RestHighLevelClient client;
public boolean existIndex(String index) {
GetIndexRequest indexRequest = new GetIndexRequest(index);
boolean exists = false;
try {
exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(exists);
return exists;
}
根据id查询文档信息
public String getDocument(String indexName,long id) {
GetRequest getRequest = new GetRequest(indexName, id+"");
try {
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> source = documentFields.getSource();
System.out.println(source);
return JSON.toJSONString(documentFields);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
批量查询文档信息
public SearchHits getBatchDocument(String indexName, int size) {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(size);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.getHits();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public SearchHits getMatchDocument(String indexName, String field, String content) {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
//这里这个条件只能加一个,再加一个会覆盖前面的内容
builder.query(QueryBuilders.matchQuery(field, content));
builder.size(10);
searchRequest.source(builder);
try {
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
return search.getHits();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public SearchHits getMatchesDocument(String indexName, Map<String, String> map) {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//逻辑删除标志必须是false,精确查询
boolQuery.must(QueryBuilders.termQuery("del_flag", "false"));
//按照时间范围查询
String beginTime = map.remove("beginTime");
String endTime = map.remove("endTime");
boolQuery.must(QueryBuilders.rangeQuery("create_time").gte(beginTime).lte(endTime));
//分页查询
int pageNo = Integer.parseInt(map.remove("pageNo"));
int pageSize = Integer.parseInt(map.remove("pageSize"));
builder.from((pageNo-1)*pageSize);
builder.size(pageSize);
map.forEach((k, v) -> {
//按照字段模糊查询
boolQuery.must(QueryBuilders.matchPhraseQuery(k, v));
});
builder.query(boolQuery);
request.source(builder);
try {
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
return searchResponse.getHits();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我们查询的结果中有"_score" : 2.8526313
这一项,分数用于判断模糊查询的匹配程度。分数越高匹配程度越高。不需要的话可以查询的时候取消评分降低性能消耗。
where id in (******) and delFlag=*
的效果注意,es根据字段多个值进行查询的时候,需要加上keyword表示不进行分词处理,用于关键词搜索,不然会出现异常的情况,查询到的结果很奇怪。用法如下:
//根据字段值进行集合查询
boolQuery.must(QueryBuilders.termsQuery(fieldName+".keyword", values));
//逻辑删除标志必须是false,精确查询
boolQuery.must(QueryBuilders.termQuery("del_flag", "false"));
总结一下涉及到的查询:
term
查询,类似=
tearms
查询,类似in
。 ElasticSearch 5.0以后,string类型有重大变更,移除了string类型,string字段被拆分成两种新的数据类型: text用于全文搜索的,而keyword用于关键词搜索。withcard
,类似like
。里面?单表单个字符,*代表多个字符range
match
,单个字段匹配一个值。multiMatch
,多个字段匹配一个值。bool
:must
必须满足条件。must_not
必须不满足。should
类似于or
,filter
关闭评分,提高查询效率。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。