一.安装Elasticsearch
(略)
二.启动Elasticsearch
浏览器访问, 看是否启动成功
三.操作与使用
命令创建
a.查看集群健康状态:http://localhost:9200/_cat/health?v
b.查看节点:http://localhost:9200/_cat/nodes?v
c.查看指数:http://localhost:9200/_cat/indices?v
d.创建索引:http://localhost:9200/_cat/indices?v
Kibana管理索引
1.安装Kibana,用来搜索、查看交互存储在Elasticsearch索引中的数据(创建节点,执行put命令)
i.官方地址:https://www.elastic.co/cn/products/kibana
ii.到kibana安装目录的config下,编辑kibana.yml配置文件,添加如下配置:
#配置本机ip
server.host: “127.0.0.1”
#配置es集群url
elasticsearch.hosts: “http://127.0.0.1:9200”
iii.启动kibana:进入bin目录双击kibana.bat,访问http://127.0.0.1:5601,看到如下界面:
Java项目中使用Elasticsearch 6.x
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adou.java.es</groupId>
<artifactId>java-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<!-- 打包跳过测试 -->
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml
elasticsearch:
hostlist:
47.110.***.***:9200 #多个节点中间用逗号分隔[addr1,addr2]
创建配置类
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
private List<String> hostlist;
@Bean
public RestHighLevelClient restHighLevelClient() {
List<HttpHost> httpHostList = new ArrayList<>(hostlist.size());
// 封装es服务端地址
for (String host : hostlist) {
HttpHost httpHost = new HttpHost(host.split(":")[0], Integer.parseInt(host.split(":")[1]), "http");
httpHostList.add(httpHost);
}
return new RestHighLevelClient(RestClient.builder(httpHostList.toArray(new HttpHost[0])));
}
// 把低级客户端也注入,但是基本不用
@Bean
public RestClient restClient() {
List<HttpHost> httpHostList = new ArrayList<>(hostlist.size());
// 封装es服务端地址
for (String host : hostlist) {
HttpHost httpHost = new HttpHost(host.split(":")[0], Integer.parseInt(host.split(":")[1]), "http");
httpHostList.add(httpHost);
}
return RestClient.builder(httpHostList.toArray(new HttpHost[0])).build();
}
public List<String> getHostlist() {
return hostlist;
}
public void setHostlist(List<String> hostlist) {
this.hostlist = hostlist;
}
}
4.对索引的增、删、改、查
/**
* 索引管理
*
* @author zhoudoujun01
* @date 2019年9月9日16:40:48
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestIndex {
@Autowired
RestHighLevelClient client;
@Autowired
RestClient restClient;
//创建索引库
@Test
public void testCreateIndex()throws Exception{
//创建索引请求对象、并设置索引名称
CreateIndexRequest createIndexRequest = new CreateIndexRequest("school");
//设置参数
Settings settings = Settings.builder().put("number_of_shards", 3)
.put("number_of_replicas",1)
.build();
createIndexRequest.settings(settings);
FileInputStream is = new FileInputStream("F:/My/Tmp/json/"+"mapping.json");
String mappingJson = IOUtils.toString(is);
System.err.println(mappingJson);
//设置映射
createIndexRequest.mapping("student",mappingJson,XContentType.JSON);
//创建索引操作对象
IndicesClient indices = client.indices();
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
//获得相应是否成功
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
// @Test
public void testSelectIndex()throws Exception{
// s穿建删除索引库请求对象
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices("school");
// 判断是否存在
Boolean isExists = client.indices().exists(getIndexRequest);
System.out.println(isExists);
}
//删除索引库
// @Test
public void testDeleteIndex()throws Exception{
//穿建删除索引库请求对象
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("school");
//删除索引库
DeleteIndexResponse deleteIndexResponse= client.indices().delete(deleteIndexRequest);
//删除结果
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
}
--------------------------------------------------------------------------------------------------
{
"properties": {
"name":{
"type":"text"
},
"address":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss|| yyy-MM-dd||epoch_millis"
}
}
}
5.对文档的增、删、改
/**
* 文档管理
*
* @author zhoudoujun01
* @date 2019年9月9日16:42:37
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestDocument {
@Autowired
private RestHighLevelClient client;
/**
* 添加文档
* 特别注意doc不要掉、否则报错org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;
*
* @throws Exception
*/
@Test
public void testAddDoc() throws Exception{
//加载准备好了的json数据
InputStream is = new FileInputStream("F:/My/Tmp/json/" + "document.json");
String docJsonStr = IOUtils.toString(is);
System.err.println(docJsonStr);
//获取索引库对象
IndexRequest indexRequest = new IndexRequest("school","student");
indexRequest.source(docJsonStr, XContentType.JSON);
//往索引库添加文档,这个动作也叫索引
IndexResponse indexResponse = client.index(indexRequest);
//打印结果
System.out.println(indexResponse.getResult());
}
/**
* 查询文档(根据id查)
* 结果:
* {
"name":"周*",
"address":"江***",
"age":22,
"date":"1995-04-12"
}
*/
// @Test
public void testGetDoc()throws Exception{
GetRequest getRequest = new GetRequest("school",
"student",
"bM7H9WsBKvN4OqQCOnFG");
GetResponse getResponse = client.get(getRequest);
if(getResponse.isExists()){
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
}
}
/**
* 更新文档
* 打印结果: OK
* 注意:这里采用的是局部更新:只修改map中设置的字段,没有的不会更新。 更新文档的实际顺序是: 检索文档、标记删除、创建新文档、删除原文档。创建新文档就会重构索引(分词-重构倒排索引树)
*
*/
// @Test
public void testUpdateDoc()throws Exception{
UpdateRequest updateRequest = new UpdateRequest("school",
"student",
"bM7H9WsBKvN4OqQCOnFG");
Map<String, String> map = new HashMap<String,String>();
map.put("name", "23");
updateRequest.doc(map);
UpdateResponse updateResponse = client.update(updateRequest);
System.out.println(updateResponse.status());
}
/**
* 删除文档
* 打印结果:DELETED
*/
// @Test
public void testDelDoc() throws Exception{
DeleteRequest deleteRequest = new DeleteRequest("school",
"student",
"bM7H9WsBKvN4OqQCOnFG");
DeleteResponse deleteResponse = client.delete(deleteRequest);
System.out.println(deleteResponse.getResult());
}
}
-------------------------------------------------------------------------------------------------
{
"name":"周*",
"address":"江苏**",
"age":22,
"date":"1995-04-12"
}
对文档的查
a.获取某个索引的某个类型的某个id的数据(以下两种方式均可)
GET /school/student/1
GET /school/student/1?_source
b. 查询指定ID的某些字段
GET /school/student/1?_source=address
这个语句相当于SELECT ADDRESS FROM STUDENT WHERE ID = 1
c 查询当前索引school的所有数据
/**
* 查询type下所有文档
对应http请求json
GET _search
{
"query": {
"match_all": {}
},
"_source": ["name","address"]
}
*/
// @Test
public void testSearchAll()throws Exception{
//1、构造sourceBuild(source源)
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[]{"name","address"}, new String[]{})
.query(QueryBuilders.matchAllQuery());
//2、构造查询请求对象:索引,类型
SearchRequest searchRequest = new SearchRequest("school");
searchRequest.types("student")
.source(searchSourceBuilder);
//3、client 执行查询
SearchResponse searchResponse = client.search(searchRequest);
//4、打印结果
SearchHits hits = searchResponse.getHits();
System.out.println(hits.getTotalHits());
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
d.按照字段内容name精确查询相关数据
/**
* term query: 精确查询、在搜索是会精确匹配关键字、搜索关键字不分词
*
* json 参数
GET lib/user/_serch
{
"query": {
"term": {"name": "周*"}
}
}
* }
*/
@Test
public void testTermQuery()throws Exception{
//1、设置queryBuilder
TermQueryBuilder termQueryBuild = QueryBuilders.termQuery("name","name");
//2、设置sourceBuilder
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(termQueryBuild)//设置Term query查询
.fetchSource(new String[]{"name","周*"}, new String[]{});
//3、构造searchRequest
SearchRequest searchRequest = new SearchRequest("school");
searchRequest.types("student")
.source(searchSourceBuilder);
//4、client发出请求
SearchResponse searchResponse = client.search(searchRequest);
//5、打印结果
SearchHits hits = searchResponse.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
e.分页查询内容
/**
* 分页查询type下所有文档
*
* json 参数
GET _search
{
"from":0,
"size":2,
"query": {
"match_all": {}
},
"_source": ["name","周*"]
}
*/
// @Test
public void testSearchAllByPage()throws Exception{
//1、构造sourceBuild
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[]{"name","name"}, new String[]{})
.query(QueryBuilders.matchAllQuery())
.from(0).size(2);//分页查询,下表从0开始
//2、构造searchRequest请求对象
SearchRequest searchRequest = new SearchRequest("school");
searchRequest.types("student")
.source(searchSourceBuilder);
//3、client执行请求
SearchResponse searchResponse = client.search(searchRequest);
//4、打印结果
SearchHits hits = searchResponse.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
f.查询内容排序(按照时间倒序排列,年龄正序排序)
/**
* 排序:
* 可以设置排序字段对查询结果进行排序
* keyword 、date 、float 等可以加
* 注意text 不能加
*
* json 参数
* {
* "_source": ["name","address","age","date],
* query{
* bool:{
* fileter: [ //过滤器也可以单独使用,但是只能用于单个字段
* {
* term: {"name": "周*"} //针对字段进行过滤
* },{
* range: { //针对范围进行过滤
* "age": {"gte":20,"lte":30}
* }
* }
* ]
* }
* },
* "sort": [
* {
* "date": "desc"
* },{
* "age": "asc"
* }
* ]
* }
*/
// @Test
public void testSort()throws Exception{
//1、构造QueryBuild
//构造booleanQueryBuilder
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//过滤
boolQueryBuilder.filter(QueryBuilders.termQuery("name", "周*"))
.filter(QueryBuilders.rangeQuery("age").gte(20).lte(30));
//2、构造查询源
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.fetchSource(new String[]{"name","address","age","date"}, new String[]{});
ssb.query(boolQueryBuilder);
ssb.sort(new FieldSortBuilder("date").order(SortOrder.DESC));
ssb.sort(new FieldSortBuilder("asc").order(SortOrder.ASC));
//3、构造请求对象查询
SearchRequest searchRequest = new SearchRequest("xc_course");
searchRequest.types("doc");
searchRequest.source(ssb);
//4、client执行查询
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
g.其它查询
……