elasticsearch5.x的java实现搜索

本项目只是作为基础入门演示elasticsearch的简单功能参考,实际部署多节点和处理实际业务的思路可以参考本博客的这两篇文章:1.elasticsearch5.x在linux上分布式安装(多节点) 2.elasticsearch用java实现分布式服务



elasticsearch5.x的java实现,本次使用的是maven工程,后面附上源码下载地址

注意:分页时如果用的from,size,那么当数据量大时,查询时会超过默认的最大数量,应该修改默认的最大数量.


 http://localhost:9200/indexdb/_settings/
put

 { "index" : { "max_result_window" : 1000000}}

elasticsearch5.x的java实现搜索_第1张图片

1.pom.xml

elasticsearch5.x的java实现搜索_第2张图片

elasticsearch5.x的java实现搜索_第3张图片

这里特别需要注意下,在加入了x-pack后,需要相应的jar包,而这个jar包在官方的maven rep的仓库中也没有这个jar,这个jar包在elastic公司自己的maven仓库上。所以建议先下载除去红色框之外的的jar包(将红框部分的注释),下载完后引用elastic的公司的maven仓库地址,然后下载x-pack-tansport.jar包。下载这jar包会非常的慢,如果想求快可以配个国外,然后在浏览器上输入那个仓库的地址去下载。

2.log4j2.properties的配置

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
 
rootLogger.level = info
rootLogger.appenderRef.console.ref = console


 

3.代码的实现

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luohao.helper.DataConvertHelper;
import com.luohao.helper.StringUtils;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

@RestController
@RequestMapping(value = "/es")
public class EsController {

private Logger logger = LoggerFactory.getLogger(EsController.class);

@RequestMapping(value = "/search.ashx", method = RequestMethod.POST)
public JSONObject queryEsearch(HttpServletRequest req, HttpServletResponse resp) throws UnknownHostException {
Map where = DataConvertHelper.getRequestParams(req);
Integer curr = Integer.valueOf(where.get("curr").toString());
JSONObject result = new JSONObject();
//1.设置集群
Settings settings = Settings.builder().put("cluster.name", "elasticsearch") // 本地固定的集群名称
.put("xpack.security.transport.ssl.enabled", false) // 设置xpack的ssl模块不可用
.put("xpack.security.user", "elastic:changeme") // 登录用户和密码,不然不能连接到elasticsearch
.put("client.transport.sniff", true) // 自动去发现新加入集群的机器
.build();

//2.获取client,如果加入了x-pack安全框架,则PreBuiltXPackTransportClient必须换成x-packjar包中的
TransportClient client = new PreBuiltXPackTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("124.225.222.53"), 9300));

// 查询条件
QueryBuilder matchQuery = QueryBuilders.multiMatchQuery(where.get("resourcename").toString(), "resourcename"); // 设置搜索条件,模糊搜
BoolQueryBuilder must = QueryBuilders.boolQuery().must(matchQuery);

if (!StringUtils.IsEmptyOrNull(where.get("stage_gid"))) {
QueryBuilder matchQueryStage = QueryBuilders.matchPhraseQuery("stage_gid", where.get("stage_gid")); // 精准搜
must.must(matchQueryStage);
}
if (!StringUtils.IsEmptyOrNull(where.get("subject_value"))) {
QueryBuilder matchQuerySubject = QueryBuilders.matchPhraseQuery("subject_value", where.get("subject_value"));
must.must(matchQuerySubject);
}
if (!StringUtils.IsEmptyOrNull(where.get("book_version"))) {
QueryBuilder matchQueryBook = QueryBuilders.matchPhraseQuery("book_version", where.get("book_version"));
must.must(matchQueryBook);
}
if (!StringUtils.IsEmptyOrNull(where.get("text_book_gid"))) {
QueryBuilder matchQueryText = QueryBuilders.matchPhraseQuery("text_book_gid", where.get("text_book_gid"));
must.must(matchQueryText);
}
if (!StringUtils.IsEmptyOrNull(where.get("type_value"))) {
QueryBuilder matchQueryType = QueryBuilders.matchPhraseQuery("type_value", where.get("type_value"));
must.must(matchQueryType);
}

// 排序
SortBuilder sortBuilder = SortBuilders.fieldSort("viewnum").order(SortOrder.DESC);// 提交时间排序

HighlightBuilder hiBuilder = new HighlightBuilder(); // 设置高亮
hiBuilder.preTags("");
hiBuilder.postTags("");
hiBuilder.field("resourcename");// 若要匹配多个高亮字段,则只用在后面加.field("字段的名称")

//3.去索引库搜索数据
SearchResponse response = client.prepareSearch("indexdb") // 需要搜索的索引库
.setTypes("tab") // 搜索的类型(相当于数据库中的表),这里如果不设置就搜索这个索引库下所有的类型,
.setQuery(must) // 搜索的条件
.addSort(sortBuilder) // 设置排序
.highlighter(hiBuilder) // 设置高亮
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH) // 设置搜索类型,各个不同的类型决定搜索所用的时间
.setFrom((curr-1)*10) // 从多少开始搜,相当于pageIndex
.setSize(10) // 搜索结果集的总数,相当于pageSize
.execute().actionGet();

//4.获取查询结果集
SearchHits searchHits = response.getHits();

//5.处理数据得到我们想要的数据
JSONArray array = new JSONArray();
for (SearchHit hit : searchHits) {
String sourceAsString = hit.getSourceAsString();
JSONObject parseObject = JSONObject.parseObject(sourceAsString);
if (!StringUtils.IsEmptyOrNull(hit.getHighlightFields().get("resourcename"))) {
Text[] text = hit.getHighlightFields().get("resourcename").getFragments();
parseObject.put("resourcename", text[0].string());
}
/*if (!StringUtils.IsEmptyOrNull(hit.getHighlightFields().get("masterial"))) {
Text[] wrap = hit.getHighlightFields().get("masterial").getFragments();
parseObject.put("masterial", wrap[0].string());
}*/
array.add(parseObject);
}
if(searchHits.getHits().length == 0) {
result.put("data", array);
result.put("total", 0);
} else {
result.put("data", array);
result.put("total", searchHits.getTotalHits());
result.put("totalpage", (searchHits.getTotalHits() + 10 -1) /10);
}
return result;
}
}


源码下载地址:http://download.csdn.net/detail/lh2420124680/9905288

你可能感兴趣的:(elasticsearch5.x的java实现搜索)