ElasticSearch:Java客户端的使用
- 使用Java客户端对ElasticSearch的增删改查的步骤:
- 创建Maven工程
- 在
pom.xml
文件中导入相应的坐标:<dependencies> <!--ES使用的jar包--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.8</version> </dependency> <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.12</version> </dependency> <!--json解析--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> </dependencies>
- 项目创建完成之后的目录结构:
- 在
- 增:
- 创建索引:
index
- 创建:创建索引
index
对象需要先进行settings
对象 和client
对象的创建:// 进行settings对象的创建 和 client对象的创建 // 创建一个settings对象 相当于一个配置信息 主要配置的是集群的名称 Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build(); // 创建一个client对象客户端对象,构造方法中需要传入settings对象作为参数 client = new PreBuiltTransportClient(settings); // 这样的方式是为了保证高可用 防止一个节点挂了 不能连接到集群中 client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302)); //使用client对象来进行索引库的创建 方法中的参数是index索引库 的名称 get 方法是执行操作的 client.admin().indices().prepareCreate("index-1").get(); // 最后的步骤是进行client对象的关闭: System.out.println("执行成功"); client.close();
- 测试 :执行成功 但是index中没有mappings信息
- 创建:创建索引
- 创建
mappings
-
创建: mappings的创建同样是需要
settings
对象 和client
对象的创建- 将settings对象 和client对象的创建封装成方法(这里使用JUnit单元测试):
@Before public void init() throws Exception{ // 进行settings对象的创建 和 client对象的创建 // 创建一个settings对象 相当于一个配置信息 主要是集群的名称 Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build(); // 创建一个client对象客户端对象,构造方法中需要传入settings对象作为参数 client = new PreBuiltTransportClient(settings); // 这样的方式是为了保证高可用 防止一个节点挂了 不能连接到集群中 client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302)); }
mappings
信息的添加:使用ES
中的XContentBuilder
进行json对象的描述(或者是直接使用json格式的字符串)@Test public void setMappings() throws Exception{ // 进行mapping信息的设置 // 使用es 中的xcontextbuilder进行json对象的描述 使用工厂对象进行生产 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") // 这里使用的分析器 能使ik_smart 或者是Ik_max_word .endObject() .startObject("content") .field("type", "text") .field("store",true) // 表示的是是否存储 .field("analyzer", "ik_smart") .endObject() .endObject() .endObject() .endObject(); // 使用client客户端将mapping信息设置到索引库中 这样还是对索引库的维护 // 方法中需要设置的参数 // 最后需要使用get方法进行执行 client.admin().indices() //索引库的名称 .preparePutMapping("index-1") // type 的名称 .setType("article") //设置需要添加的信息 能使xContentBuilder对象 也能是json格式的字符串类型 .setSource(xContentBuilder).get(); // 关闭连接:使用close方法 System.out.println("执行成功"); client.close(); }
- 将settings对象 和client对象的创建封装成方法(这里使用JUnit单元测试):
-
测试:mappings信息添加成功
-
- 创建
Document
对象:两种方式- 方式一:使用
jackson
将Java对象解析成json格式的字符串- 先将实体类对象进行创建: 创建
Article
实体类package com.itheima.pojo; public class Article { private Long id; private String title; private String content; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
- 使用
jackson
进行对象的转换 在进行Document
文档的添加:@Test public void addDocument() throws Exception{ // 使用article对象进行文档的添加 使用String --转json 使用的是jackson // 创建一个article对象 Article article = new Article(); article.setId(3L); article.setTitle("zhangsan"); article.setContent("lisi"); // 使用jackson 进行对象的转换 ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(article); System.out.println(json); // 使用client对象将json写入索引库 client.prepareIndex("index-1", "article", "3").setSource(json, XContentType.JSON).get(); // 将client对象进行关闭 client.close(); }
- 先将实体类对象进行创建: 创建
- 方式二: 使用
xcontextbuilder
进行json的描述@Test public void addDocument2Test() throws Exception{ // 同样的是需要创建一个client对象 添加文档的第二种方式 // 首先是需要使用xcontextbuilder进行json的描述 XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() .startObject() .field("id", 4L) .field("title", "今天的因特网无疑是有史以来由人类创造的、精心设计的最大系统、") .field("content", "Jack和大家一起来重温《Java数据结构》经典之作。第一部分:Java数据结构 要理解Java数据结构,必须") .endObject(); // 使用client对象将文档添加到索引库中 不设置id es会自动的设置id client.prepareIndex("index-1", "article", "2").setSource(xContentBuilder).get(); // 将client进行关闭 client.close(); }
- 测试:
- 方式一:使用
- 创建索引:
- 查:ES中查询的三种方式:(和Lucene中一致)
- 根据
_id
进行查询 :@Test public void queryById(){ // 进行查询的步骤: // 1 创建一个client对象 // 2 创建query查询对象 这里是queryBuilders工具类 进行QueryBuilder(不同的查询方式使用的对象不同) QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("2");// 能传入多个id search(queryBuilder); }
- 根据关键词
Term
进行查询@Test public void testQueryByTerm(){ // 关键字进行查询 方法中要指定要搜所的域 要制定的关键词 QueryBuilder queryBuilder = QueryBuilders.termQuery("article", "经典"); search(queryBuilder); }
- 根据
QueryString
进行查询 (先进行分词 在进行查询 这样的方式最常使用)@Test public void testQueryString(){ // 测试关键字QueryString进行查询 就是想QueryParser先进行分词在进行查询 参数就是需要查询的句子 还能指定默认的搜索域 QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("速度") .defaultField("title");// 进行默认的搜索域的设置 search(queryBuilder); }
- 根据
- 创建Maven工程