pom.xml坐标
<dependencies>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>5.6.8version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>5.6.8version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-to-slf4jartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.24version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-simpleartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.12version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
@Test
public void createIndex() throws Exception {
//创建一个Setings对象 相当于是一个配置信息,主要配置集群的名称
Settings build = Settings.builder()
.put("cluster.name","my-elasticsearch")
.build();
///创建一个客户端Client对象
TransportClient client = new PreBuiltTransportClient(build);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9303));
/// 创建client对象创建索引库 //执行操作
client.admin().indices().prepareCreate("index_hello").get();
//关闭client对象
client.close();
}
public void setMappings() throws Exception {
//创建一个Settings对象
Settings settings = Settings.builder()
.put("cluster.name", "my-elasticsearch")
.build();
///创建一个TransportClient对象
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9303));
//创建一个Mappings信息
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type","long")
.field("store",true)
.endObject()
.startObject("title")
.field("type","text")
.field("store",true)
.field("analyzer","ik_smart")
.endObject()
.startObject("content")
.field("type","text")
.field("store",true)
.field("analyzer","ik_smart")
.endObject()
.endObject()
.endObject()
.endObject();
//使用client把mapping信息设置到索引中
client.admin().indices()
//设置要做映射的索引
.preparePutMapping("index_hello")
//设置要做映射的type
.setType("article")
//mapping信息,可以是XContentBuilder对象也可以是字符串也可以是json格式
.setSource(xContentBuilder)
//执行操作
.get();
//关闭客户端
client.close();
}
//添加文档
@Test
public void testAddDocument() throws Exception {
//创建一个client对象
//创建一个文档对象
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("id",1l)
.field("title","北方入秋速度明显加快,多地降温幅度最多可达10度")
.field("content","阿联酋一架客机在纽约基础被隔离,10名乘客病例")
.endObject();
//把文档对象添加到索引库
///设置索引名称 type 设置文档id,如果不设置的话自动的生成一个id 设置文档信息
client.prepareIndex("index_hello","article","1").setSource(xContentBuilder).get();
//关闭客户端
client.close();
}
1)创建Article实体
public class Article {
private Integer id;
private String title;
private String content;
getter/setter...
}
2)添加jackson坐标
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.8.1version>
dependency>
@Test
public void testAddDocument2() throws Exception {
///创建Article对象
Article article = new Article();
//设置对象的属性
article.setId(3);
article.setTitle("MH370坠毁在柬埔寨密林?");
article.setContent("警惕荒唐的死亡游戏!饿15岁少年");
//把article对象转换成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//关闭客户端
client.prepareIndex("index_hello","article","3").setSource(jsonDocument, XContentType.JSON).get();
client.close();
}
@Test
public void testSearchById() throws Exception {
// 创建一个client对象
//创建一个查询对象
QueryBuilder QueryBuilder = QueryBuilders.idsQuery().addIds("1","2");
//执行查询
SearchResponse searchResponse = client.prepareSearch("index_hello").setTypes("article").setQuery(QueryBuilder).get();
//取查询结果
SearchHits hits = searchResponse.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数:"+hits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
//打印文档对象,以json格式输出
System.out.println(next.getSourceAsString());
//取文档的属性
System.out.println("文档的属性");
Map<String,Object> document = next.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//关闭client
client.close();
}
@Test
public void testQueryByTerm() throws Exception{
///创建一个QueryBuilder对象
// name 要搜索的字段 value 要搜索的关键词
QueryBuilder queryBuilder = QueryBuilders.termQuery("title","北方");
search(queryBuilder);
}
@Test
public void testQueryStringQuery() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(“速度与激情”).defaultField(“title”); //默认搜索域 defaultField
search(queryBuilder);
}
@Test
public void testQueryStringQuery1() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("打算").defaultField("title"); //默认搜索域 defaultField
search1(queryBuilder);
}
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
百度搜索关键字"传智播客"
通过开发者工具查看高亮数据的html代码实现:
ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹
//设置高亮显示
@Test
public void testQueryStringQuery3() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(“打算”).defaultField(“title”); //默认搜索域 defaultField
search2(queryBuilder,“title”);
}
package com.itheima.es;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesAdminClient;
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.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.IdsQueryBuilder;
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.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
public class ElasticSearchClientTest {
private TransportClient client;
@Before
public void init() throws Exception {
//创建一个Setings对象 相当于是一个配置信息,主要配置集群的名称
Settings build = Settings.builder()
.put("cluster.name","my-elasticsearch")
.build();
///创建一个客户端Client对象
client = new PreBuiltTransportClient(build);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9303));
}
@Test
public void createIndex() throws Exception {
创建一个Setings对象 相当于是一个配置信息,主要配置集群的名称
//Settings build = Settings.builder()
// .put("cluster.name","my-elasticsearch")
// .build();
/创建一个客户端Client对象
//TransportClient client = new PreBuiltTransportClient(build);
//client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));
//client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9302));
//client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9303));
/// 创建client对象创建索引库 //执行操作
client.admin().indices().prepareCreate("index_hello").get();
//关闭client对象
client.close();
}
@Test
public void setMappings() throws Exception {
//创建一个Settings对象
//Settings settings = Settings.builder()
// .put("cluster.name", "my-elasticsearch")
// .build();
/创建一个TransportClient对象
//TransportClient client = new PreBuiltTransportClient(settings)
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9301))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9302))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9303));
//创建一个Mappings信息
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type","long")
.field("store",true)
.endObject()
.startObject("title")
.field("type","text")
.field("store",true)
.field("analyzer","ik_smart")
.endObject()
.startObject("content")
.field("type","text")
.field("store",true)
.field("analyzer","ik_smart")
.endObject()
.endObject()
.endObject()
.endObject();
//使用client把mapping信息设置到索引中
client.admin().indices()
//设置要做映射的索引
.preparePutMapping("index_hello")
//设置要做映射的type
.setType("article")
//mapping信息,可以是XContentBuilder对象也可以是字符串也可以是json格式
.setSource(xContentBuilder)
//执行操作
.get();
//关闭客户端
client.close();
}
//添加文档
@Test
public void testAddDocument() throws Exception {
//创建一个client对象
//创建一个文档对象
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("id",2l)
.field("title","北方入秋速度明显加快,多地降温幅度最多可达10度22222")
.field("content","阿联酋一架客机在纽约基础被隔离,10名乘客病例")
.endObject();
//把文档对象添加到索引库
///设置索引名称 type 设置文档id,如果不设置的话自动的生成一个id 设置文档信息
client.prepareIndex("index_hello","article","2").setSource(xContentBuilder).get();
//关闭客户端
client.close();
}
@Test
public void testAddDocument2() throws Exception {
///创建Article对象
Article article = new Article();
//设置对象的属性
article.setId(3);
article.setTitle("MH370坠毁在柬埔寨密林?");
article.setContent("警惕荒唐的死亡游戏!饿15岁少年");
//把article对象转换成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//关闭客户端
client.prepareIndex("index_hello","article","3").setSource(jsonDocument, XContentType.JSON).get();
client.close();
}
@Test
public void testAddDocument3() throws Exception {
for (int i = 4; i < 100; i++) {
///创建Article对象
Article article = new Article();
//设置对象的属性
article.setId(i);
article.setTitle("成龙阿萨德阿达撒打算打是的撒是的撒"+i);
article.setContent("成龙阿萨德阿达撒打算打是的撒是的撒"+i);
//把article对象转换成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//关闭客户端
client.prepareIndex("index_hello","article",i+"").setSource(jsonDocument, XContentType.JSON).get();
}
client.close();
}
private void search(QueryBuilder QueryBuilder) throws Exception {
//执行查询
SearchResponse searchResponse = client.prepareSearch("index_hello").setTypes("article").setQuery(QueryBuilder).get();
//取查询结果
SearchHits hits = searchResponse.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数:"+hits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
//打印文档对象,以json格式输出
System.out.println(next.getSourceAsString());
//取文档的属性
System.out.println("文档的属性");
Map<String,Object> document = next.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//关闭client
client.close();
}
private void search1(QueryBuilder QueryBuilder) throws Exception {
//执行查询 设置分页信息
SearchResponse searchResponse = client.prepareSearch("index_hello").setTypes("article").setQuery(QueryBuilder).setFrom(0).setSize(5).get();
//取查询结果
SearchHits hits = searchResponse.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数:"+hits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
//打印文档对象,以json格式输出
System.out.println(next.getSourceAsString());
//取文档的属性
System.out.println("文档的属性");
Map<String,Object> document = next.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//关闭client
client.close();
}
private void search2(QueryBuilder QueryBuilder, String highlightFiled) throws Exception {
HighlightBuilder highlightBuilder = new HighlightBuilder();
//高亮显示的字段
highlightBuilder.field("title");
highlightBuilder.preTags("");
highlightBuilder.postTags("");
//执行查询 设置分页信息
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(QueryBuilder)
.setFrom(0)
.setSize(5)
//设置高亮信息
.highlighter(highlightBuilder)
.get();
//取查询结果
SearchHits hits = searchResponse.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数:"+hits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
//打印文档对象,以json格式输出
System.out.println(next.getSourceAsString());
//取文档的属性
System.out.println("文档的属性");
Map<String,Object> document = next.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
System.out.println("*************");
Map<String, HighlightField> highlightFields = next.getHighlightFields();
System.out.println(highlightFields); //{title=[title], fragments[[成龙阿萨德阿达撒打算打是的撒是的撒4]]}
//取高亮显示的结果
HighlightField field = highlightFields.get(highlightFiled);
Text[] fragments = field.getFragments();
if (fragments !=null){
String title = fragments[0].toString();
System.out.println(title);
}
}
//关闭client
client.close();
}
@Test
public void testSearchById() throws Exception {
// 创建一个client对象
//创建一个查询对象
QueryBuilder QueryBuilder = QueryBuilders.idsQuery().addIds("1","2");
search(QueryBuilder);
}
@Test
public void testQueryByTerm() throws Exception{
///创建一个QueryBuilder对象
// name 要搜索的字段 value 要搜索的关键词
QueryBuilder queryBuilder = QueryBuilders.termQuery("title","北方");
search(queryBuilder);
}
@Test
public void testQueryStringQuery() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("速度与激情").defaultField("title"); //默认搜索域 defaultField
search(queryBuilder);
}
@Test
public void testQueryStringQuery1() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("打算").defaultField("title"); //默认搜索域 defaultField
search1(queryBuilder);
}
//设置高亮显示
@Test
public void testQueryStringQuery3() throws Exception{
//创建一个QueryBuilder对象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("打算").defaultField("title"); //默认搜索域 defaultField
search2(queryBuilder,"title");
}
}