一、通過原生的Java代碼操作ES
(1)運行環(huán)境:idea、maven3.5、spring
(2)pom.xml 文件中需要配置的工具包
? ? <maven.compiler.source>1.8
? ? <maven.compiler.target>1.8
? ? ? ? <!--測試類架包-->
? ? ? ? ? ? <groupId>junit
? ? ? ? ? ? <artifactId>junit
? ? ? ? ? ? <version>4.12
? ? ? ? ? ? <scope>test
? ? ? ? <!--ES 相關(guān)包-->
? ? ? ? ? ? <groupId>org.elasticsearch
? ? ? ? ? ? <artifactId>elasticsearch
? ? ? ? ? ? <version>5.6.1
? ? ? ? ? ? <groupId>org.elasticsearch.client
? ? ? ? ? ? <artifactId>transport
? ? ? ? ? ? <version>5.6.1
? ? ? ? <!--日志相關(guān)包-->
? ? ? ? ? ? <groupId>org.apache.logging.log4j
? ? ? ? ? ? <artifactId>log4j-to-slf4j
? ? ? ? ? ? <version>2.10.0
? ? ? ? ? ? <groupId>org.apache.logging.log4j
? ? ? ? ? ? <artifactId>log4j-core
? ? ? ? ? ? <version>2.9.0
? ? ? ? ? ? <groupId>org.slf4j
? ? ? ? ? ? <artifactId>slf4j-api
? ? ? ? ? ? <version>1.7.25
? ? ? ? ? ? <groupId>org.slf4j
? ? ? ? ? ? <artifactId>slf4j-simple
? ? ? ? ? ? <version>1.7.21
? ? ? ? ? ? <groupId>log4j
? ? ? ? ? ? <artifactId>log4j
? ? ? ? ? ? <version>1.2.12
? ? ? ? <!--多導(dǎo)入一個包,否則報日志錯誤-->
? ? ? ? ? ? <groupId>org.apache.logging.log4j
? ? ? ? ? ? <artifactId>log4j-api
? ? ? ? ? ? <version>2.9.1
? ? ? ? <!--json數(shù)據(jù)相關(guān)包-->
? ? ? ? ? ? <groupId>com.fasterxml.jackson.core
? ? ? ? ? ? <artifactId>jackson-core
? ? ? ? ? ? <version>2.8.1
? ? ? ? ? ? <groupId>com.fasterxml.jackson.core
? ? ? ? ? ? <artifactId>jackson-databind
? ? ? ? ? ? <version>2.8.1
? ? ? ? ? ? <groupId>com.fasterxml.jackson.core
? ? ? ? ? ? <artifactId>jackson-annotations
? ? ? ? ? ? <version>2.8.1
? ? ? ? <!--spring Data框架相關(guān)包-->
? ? ? ? ? ? <groupId>org.springframework.data
? ? ? ? ? ? <artifactId>spring-data-elasticsearch
? ? ? ? ? ? <version>3.0.5.RELEASE
? ? ? ? ? ? ? ? ? ? <groupId>org.elasticsearch.plugin
? ? ? ? ? ? ? ? ? ? <artifactId>transport-netty4-client
? ? ? ? ? ? <groupId>org.springframework
? ? ? ? ? ? <artifactId>spring-test
? ? ? ? ? ? <version>5.0.5.RELEASE
</project>
(3)在resources文件夾創(chuàng)建applicationContext.xml配置文件
??????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??????xmlns:context="http://www.springframework.org/schema/context"
??????xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
??????xsi:schemaLocation="http://www.springframework.org/schema/beans
???????? http://www.springframework.org/schema/beans/spring-beans.xsd
???????? http://www.springframework.org/schema/context
???????? http://www.springframework.org/schema/context/spring-context.xsd
???http://www.springframework.org/schema/data/elasticsearch
???http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
???????????????????????????????????cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>
</beans>
(4)創(chuàng)建一個實體(構(gòu)建 :無參構(gòu)造、set、get方法)
@Document(indexName ="es_blog",type = "article")
public class Article {
???@Id
???@Field(type = FieldType.Long,store=true)
???private long id;
???@Field(type = FieldType.text,store=true,analyzer="ik_smart")
???private String title;
(5)創(chuàng)建一個接口
package com.xcy.repositories;
import com.xcy.pojo.Article;
importorg.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ArticleRepository extendsElasticsearchRepository {
}
(6)測試
1.創(chuàng)建一個索引庫

2.向索引庫中插入數(shù)據(jù)

結(jié)果:

3.刪除庫中的數(shù)據(jù)

4.查詢
