第一章 ElasticSearch简介
1. 什么是ElasticSearch
是基于Lucene开发的全文检索服务器
2. ElasticSearch的使用案例
第二章 ElasticSearch安装与启动
1. es端口
端口9300:TCP端口,使用API对服务器进行管理的端口。
端口9200:restful形式的端口,提供HTTP服务。
由于对外提供了http服务,可以通过浏览器来访问es。
2. 安装ES的图形化界面插件
启动node:
(1)需要连接9200的服务,对ES服务进行管理。
跨域:在9100访问9200的服务,那么我们需要在ES的配置文件中进行配置允许跨域访问。
允许跨域访问:*代表所有域名都访问。
(2)再次点击 连接2
创建索引:
设置mapping映射:
向索引库中添加文档:
第三章 ElasticSerach相关概念
1. ES核心概念
索引类似于一个数据库;type类似于表
第四章 ES的客户端操作
1. 创建文档
2. ES没有修改的概念,更新的本质是:先删除再添加
五、使用Java客户端管理ES
1. 创建索引库Index
代码
package com.allinpay;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;
/**
* @author zhou
* @create 2020/8/2
*/
public class esClientTest {
@Test
public void createIndex() throws Exception {
//1.创建一个Settings 对象,相当于是一个配置信息,主要是配合着集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2.创建一个客户端client对象
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9301)); //TCP
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9302)); //TCP
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9303)); //TCP
//3.使用client对象创建一个索引库
client.admin().indices().prepareCreate("index_hello").get();
//4.关闭client对象
client.close();
}
}
2. 使用Java客户端设置Mappings
3. 添加文档
也可以使用Jacksson将java对象转换成json,向索引库添加文档。
package com.allinpay;
import com.allinpay.pojo.Article;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
/**
* @author zhou
* @create 2020/8/2
*/
public class esClientTest {
private TransportClient client;
@Before
public void init() throws Exception {
//1.创建一个Settings 对象,相当于是一个配置信息,主要是配合着集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2.创建一个客户端client对象
client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9301)); //TCP
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9302)); //TCP
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9303)); //TCP
}
@Test
public void createIndex() throws Exception {
//3.使用client对象创建一个索引库
client.admin().indices().prepareCreate("index_hello").get();
//4.关闭client对象
client.close();
}
@Test
public void setMappings() throws Exception {
//3.创建一个Mapping信息
/**
* {
* "article":{
* "properties":{
* "id":{
* "type":"long",
* "store":true
* },
* "title":{
* "type":"text",
* "store":true,
* "analyzer":"ik_smart"
* },
* "content":{
* "type":"text",
* "store":true,
* "analyzer":"ik_smart"
* }* }
* }
* }
*/
XContentBuilder builder = 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();
//4.使用client把mapping信息设置到索引库
client.admin().indices()
//设置要做映射的索引
.preparePutMapping("index_hello")
//设置要做映射的type
.setType("article")
//设置mapping信息,可以是XContentBuilder对象,也可以是json字符串
.setSource(builder)
.get();
//5.
client.close();
}
@Test
public void testAddDocument() throws Exception {
//1.创建一个client对象
//2.创建一个文档对象
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id", 1l)
.field("title", "练兵魔力")
.field("content", "出鞘")
.endObject();
//3.把文档对象添加到索引库
client.prepareIndex()
//设置索引名称
.setIndex("index_hello")
//设置type
.setType("article")
//设置文档的id,如果不设置会自动生成一个id
.setId("1")
//设置文档信息
.setSource(builder)
//执行操作
.get();
client.close();
}
@Test
public void testAddDocument2() throws Exception {
Article article = new Article();
article.setId(3l);
article.setTitle("通联");
article.setContent("小铺商城");
//把对象转换成json格式字符串
ObjectMapper mapper = new ObjectMapper();
String jsonDocument = mapper.writeValueAsString(article);
System.out.println(jsonDocument);
//使用client对象把文档写入索引库
client.prepareIndex("index_hello", "article", "3")
.setSource(jsonDocument, XContentType.JSON)
.get();
client.close();
}
}
4. 查询文档操作