package es.service;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EsService {
@Autowired
private Client client;
/**
* 全文检索 模糊查询所有索引的索引字段
* @param query
* @return
*/
public SearchResponse multiBatch(String query) {
MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query,"name","address");
// 高亮
// HighlightBuilder highlightBuilder = new HighlightBuilder().field("name")//字段名
// .fragmentSize(200).highlightFilter(true);
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<h2>");
highlightBuilder.postTags("</h2>");
highlightBuilder.field("name");
SearchRequestBuilder requestBuilder = client.prepareSearch("codetype_index_ik","position_index")//"codetype_index_ik","position_index" "people","companyindex","oracleall_index"可以指定多个index 可以不传查询所有索引
.setTypes("position_type","code_type_ik") //也可以指定多个type
// .addSort("id", SortOrder.ASC)//按照id升序排列
// .setQuery(QueryBuilders.matchQuery("name", query))
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)//
// .setExplain(true)//匹配度高的排在前面
.setQuery(queryBuilder)
.highlighter(highlightBuilder)
.setFrom(0).setMinScore(1).setSize(30); //设置只保留score>1的结果,并且最多保留100个
//.setFrom(0).setSize(100)//只设置保留个数
// .highlighter(highlightBuilder);
SearchResponse searchResponse = requestBuilder.get();
return searchResponse;
}
/**
* matchPhraseQuery 搜索的内容要完全匹配 搜索“saa” 搜索内容中必须含有“saa”这个内容
*/
public SearchResponse matchPhraseQuery(String query) {
MatchPhrasePrefixQueryBuilder match = new MatchPhrasePrefixQueryBuilder("address",query);
SearchRequestBuilder requestBuilder = client.prepareSearch()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)//
.setExplain(true)//匹配度高的排在前面
.setQuery(match)
// .setQuery(QueryBuilders.termQuery("name", query))
.setFrom(0).setMinScore(1).setSize(30); //设置只保留score>1的结果,并且最多保留100个
SearchResponse searchResponse = requestBuilder.get();
return searchResponse;
}
/**
* //terms查询
*/
public List<String> terms(String query) {
List<String> list = new ArrayList<>();
TermsQueryBuilder builder = QueryBuilders.termsQuery("name","address",query);
SearchResponse response = client.prepareSearch()
.setQuery(builder)
.setSize(2)
.get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits){
list.add(hit.getSourceAsString());
}
return list;
}
//对所有字段分词查询(queryStringQuery)
public SearchResponse query(String query) {
// 1 条件查询
SearchRequestBuilder response = client.prepareSearch()
.setTypes()
.setQuery(QueryBuilders.queryStringQuery(query))
.setFrom(0)
.setMinScore(1)
.setSize(30);
SearchResponse searchResponse = response.get();
// 2 打印查询结果
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
return searchResponse;
}
//通配符查询(wildcardQuery)指定一个字段“content”不能多字段查询
public SearchResponse wildcardQuery(String query) {
// 1 条件查询
SearchRequestBuilder response = client.prepareSearch()
.setTypes()
.setQuery(QueryBuilders.wildcardQuery("content", query))
.setFrom(0)
.setMinScore(1)
.setSize(30);
SearchResponse searchResponse = response.get();
// 2 打印查询结果
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
return searchResponse;
}
public void BoolSearch(){
//多条件设置
MatchPhraseQueryBuilder mpq1 = QueryBuilders
.matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301");
MatchPhraseQueryBuilder mpq2 = QueryBuilders
.matchPhraseQuery("inputtime","2016-07-21 00:00:01");
QueryBuilder qb2 = QueryBuilders.boolQuery()
.must(mpq1)
.must(mpq2);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(qb2);
//System.out.println(sourceBuilder.toString());
//查询建立
SearchRequestBuilder responsebuilder = client.prepareSearch("pointdata")
.setTypes("pointdata");
SearchResponse myresponse=responsebuilder
.setQuery(qb2)
.setFrom(0).setSize(50)
.addSort("inputtime", SortOrder.ASC)
//.addSort("inputtime", SortOrder.DESC)
.setExplain(true).execute().actionGet();
SearchHits hits = myresponse.getHits();
for(int i = 0; i < hits.getHits().length; i++) {
System.out.println(hits.getHits()[i].getSourceAsString());
}
}
public void testSearch() {
String index="index_log_daily";
String type="uq_log";
String uquestion = "139";
String province = "江苏";
SearchResponse searchResponse = client.prepareSearch(index)
.setTypes(type)
.setQuery(QueryBuilders.matchAllQuery()) //查询所有
.setQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("uquestion", uquestion))//查询uquestion为139的
.must(QueryBuilders.matchQuery("province", province)))//查询省份为江苏的
//.setQuery(QueryBuilders.matchQuery("uquestion", "12599").operator(Operator.AND)) //根据tom分词查询name,默认or
//.setQuery(QueryBuilders.matchQuery("province", "江苏").operator(Operator.AND)) //根据tom分词查询name,默认or
//.setQuery(QueryBuilders.multiMatchQuery("tom", "name", "age")) //指定查询的字段
//.setQuery(QueryBuilders.queryString("name:to* AND age:[0 TO 19]")) //根据条件查询,支持通配符大于等于0小于等于19
//.setQuery(QueryBuilders.termQuery("name", "tom"))//查询时不分词
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0).setSize(10)//分页
//.addSort("age", SortOrder.DESC)//排序
.get();
S
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
java连接elasticsearch实现全文检索,并且高亮显示结果,实现分页。项目基于elasticsearch5.6.1可根据自己实际情况调整版本。最近项目需要所以学习了一下,项目很简单可以根据你的实际情况更改接口。我本地的搜索是可以拼音和分词(需要插件可以看我另一个资源),你们搜索的时候可以先将elasticsearch配置完善,java使用的时候会顺心很多。
资源推荐
资源详情
资源评论
收起资源包目录
Es-Test.zip (47个子文件)
Es-Test
.gitignore 268B
.project 1KB
.mvn
wrapper
maven-wrapper.jar 47KB
maven-wrapper.properties 116B
.DS_Store 6KB
pom.xml 4KB
mvnw 9KB
target
test-classes
com
example
.DS_Store 6KB
demo
EsTestApplicationTests.class 628B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
classes
es
utils
Page.class 3KB
EsTestApplication.class 681B
controller
EsController.class 5KB
HighlightController.class 7KB
config
EsConfig.class 2KB
.DS_Store 6KB
service
EsService.class 8KB
META-INF
maven
com.example
Es-Test
pom.xml 4KB
pom.properties 217B
MANIFEST.MF 320B
.DS_Store 6KB
application.properties 0B
.settings
org.eclipse.core.resources.prefs 150B
org.eclipse.jdt.core.prefs 238B
org.eclipse.wst.common.project.facet.core.xml 141B
org.eclipse.m2e.core.prefs 93B
src
test
java
com
example
.DS_Store 6KB
demo
EsTestApplicationTests.java 333B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
main
resources
application.properties 0B
java
es
utils
Page.java 3KB
controller
HighlightController.java 5KB
EsController.java 3KB
config
EsConfig.java 1KB
.DS_Store 6KB
service
EsService.java 8KB
EsTestApplication.java 294B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
mvnw.cmd 6KB
.classpath 1KB
.DS_Store 8KB
共 47 条
- 1
资源评论
小伙子很年轻
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- YOLO-yolo资源
- 适用于 Java 项目的 Squash 客户端库 .zip
- 适用于 Java 的 Chef 食谱.zip
- Simulink仿真快速入门与实践基础教程
- js-leetcode题解之179-largest-number.js
- js-leetcode题解之174-dungeon-game.js
- Matlab工具箱使用与实践基础教程
- js-leetcode题解之173-binary-search-tree-iterator.js
- js-leetcode题解之172-factorial-trailing-zeroes.js
- js-leetcode题解之171-excel-sheet-column-number.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功