windows10安装Elasticsearch与head-master教程参考另外一篇文章
文章链接 : windows10安装ElasticSearch与Head-master+IK分词器安装与自定义词库
1. ElasticSearch操作入门
创建Maven工程 pom.xml导入坐标依赖,除了第一个外,剩下都是日志依赖,可以不用
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
1.1 新建索引
package com.jwc.test;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.net.InetAddress;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-07-30 10:07
*/
public class TestES {
/*
* 使用默认方式创建文档
* */
@Test
public void testCreateDoc()throws Exception{
// 创建客户端访问对象 EMPTY表示使用默认设置
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY)
// 使用对象方式设置客户端地址 和 端口号
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 创建文档内容 json {id:1,title:"这个是title",content:"这是个content"}
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("id",1)
.field("title","这是个title")
.field("content","这是个content")
.endObject();
// 客户端发送文档到服务器,必须调用get 才能够获取到Response对象 ,不调用get不执行,如果不指定ID 那么是随机字符串`
IndexResponse indexResponse = transportClient.prepareIndex("blog", "article", "1").setSource(xContentBuilder).get();
RestStatus status = indexResponse.status();
System.out.println(status);
// 关闭客户端资源
transportClient.close();
}
}
1.2 查询所有
package com.jwc.test;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.net.InetAddress;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-07-30 10:07
*/
public class TestES {
/*
* 查询所有
* */
@Test
public void queryAll()throws Exception{
// 创建客户端对象
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 创建查询,查询的索引是 blog type是 article
SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
// 使用查询抽象接口查询所有 .get 表示执行
.setQuery(QueryBuilders.matchAllQuery()).get();
// 获取查询结果
SearchHits hits = response.getHits();
// 获取总条数
System.out.println(hits.getTotalHits());
// 获取查询内容
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭资源
transportClient.close();
}
}
1.3 字符串查询
package com.jwc.test;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.net.InetAddress;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-07-30 10:07
*/
public class TestES {
/*
* 按照字符串查询
* */
@Test
public void queruString()throws Exception{
// 创建客户端对象
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 创建查询,查询的索引是 blog type是 article
SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
// 使用查询抽象接口进行字符串查询 .get 表示执行
.setQuery(QueryBuilders.queryStringQuery("这是")).get();
// 获取查询结果
SearchHits hits = response.getHits();
// 获取总条数
System.out.println(hits.getTotalHits());
// 获取查询内容
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭资源
transportClient.close();
}
}
1.4 词条查询
package com.jwc.test;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.net.InetAddress;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-07-30 10:07
*/
public class TestES {
/*
* 按照词条查询
* */
@Test
public void queryTerm()throws Exception{
// 创建客户端对象
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 创建查询,查询的索引是 blog type是 article
SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
// 使用查询抽象接口进行词条查询 .get 表示执行
.setQuery(QueryBuilders.termQuery("title","这是")).get();
// 获取查询结果
SearchHits hits = response.getHits();
// 获取总条数
System.out.println(hits.getTotalHits());
// 获取查询内容
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭资源
transportClient.close();
}
}
1.5 通配符查询/模糊查询
package com.jwc.test;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.net.InetAddress;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-07-30 10:07
*/
public class TestES {
/*
* 通配符查询
* */
@Test
public void queryWildCard()throws Exception{
// 创建客户端对象
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 创建查询,查询的索引是 blog type是 article
SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
// 使用查询抽象接口进行通配符查询 .get 表示执行
.setQuery(QueryBuilders.wildcardQuery("title","*这是")).get();
// 获取查询结果
SearchHits hits = response.getHits();
// 获取总条数
System.out.println(hits.getTotalHits());
// 获取查询内容
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭资源
transportClient.close();
}
}