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
小伙子很年轻
- 粉丝: 0
- 资源: 5
最新资源
- qimo_text.zip
- 3CDaemon-FTP、syslog、TFTP服务器模拟程序
- 2024年企业级聊天机器人应用与优化指南
- 新能源汽车行业2025年度策略:行业触底回升,新技术加速落地.pdf
- 中国银河-钢铁行业深度报告:供需格局改善,行业产能优化强者更强.pdf
- 电力设备及新能源行业2025年年度投资策略:行业触底,复苏在即.pdf
- OTA行业深度报告:春暖花开,奔赴山海.pdf
- AI深度洞察系列报告(三):Scale up与Scaleout组网变化趋势如何看?.pdf
- 玛莎拉蒂年会活动方案.pdf
- 提升企业开源开发有效性和影响力的路线图 .pdf
- 推动应用创新的九大 AI 趋势.pdf
- 欧洲的开源成熟度:2024年的里程碑、机遇与路径研究报告(英文版).pdf
- 2024年量子技术研究报告:投资于拐点(英文版).pdf
- 2024年地中海南部和东部(SEMED)新就业形态与平台工作研究报告(英文版).pdf
- 2024年环境经济核算体系-生态系统核算报告(英文版).pdf
- 2024年东南亚的可持续航空燃料基于生物的解决办法的区域视角报告(英文版).pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈