package com.hope.es;
import org.elasticsearch.action.search.SearchResponse;
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.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
/**
* @author newcityman
* @date 2020/1/16 - 23:56
*/
public class SearchIndex {
private TransportClient client;
@Before
public void init() throws Exception {
//1、创建一个setting对象
Settings settings = Settings.builder().
put("cluster.name", "my‐elasticsearch").build();
//2、创建一个TransportClient对象,
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
public void search(QueryBuilder queryBuilder) throws Exception {
//3、执行查询结果
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
//设置分页信息
.setFrom(0)
//每页显示的页数
.setSize(5)
.get();
//4、取查询结果
SearchHits searchHits = searchResponse.getHits();
//5、取查询结果的总记录数
System.out.println("总记录数:" + searchHits.getTotalHits());
//6、查询结果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文档对象,以json格式输出
System.out.println(searchHit.getSourceAsString());
//取文档的属性
System.out.println("+++++++++++++++++++文档的属性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//7、关闭client
client.close();
}
/**
* 根据id查询
*/
@Test
public void testSearchById() throws Exception {
//1、创建一个client对象
//2、创建一个查询对象
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("4", "5", "6");
//执行查询
search(queryBuilder);
}
/**
* 根据关键词查询
* @throws Exception
*/
@Test
public void testSearchByTerm() throws Exception{
//创建client对象
//创建查询对象
//参数一:要搜索的字段
//参数二:要搜索的关键词
QueryBuilder queryBuilder = QueryBuilders.termQuery("content", "政府");
search(queryBuilder);
}
@Test
public void testQueryStringQuery() throws Exception{
//创建一个QueryBuilder对象
QueryStringQueryBuilder query = QueryBuilders.queryStringQuery("澳洲火气大")
.defaultField("title");
//执行查询
search(query,"title");
}
public void search(QueryBuilder queryBuilder,String highlightField) throws Exception {
HighlightBuilder highlightBuilder = new HighlightBuilder();
//高亮显示的字段
highlightBuilder.field(highlightField);
//高亮显示的字段前缀
highlightBuilder.preTags("<em>");
highlightBuilder.postTags("</em>");
//3、执行查询结果
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
//设置分页信息
.setFrom(0)
//每页显示的页数