一. 环境配置
- IntelliJ IDEA 2018.2
- java1.8
- Maven3
二. 创建项目
1.创建一个maven项目,并配置es的maven环境
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.创建完成的目录
三. java操作es
1.连接es服务器
private TransportClient client;
@Before
public void init() throws UnknownHostException {
//指定ES集群
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//创建访问es服务器的客户端
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.239.53"), 9300));
}
cluster.name为固定名称,my-application 到es服务器 elasticsearch-6.4.0/config/elasticsearch.yml
2.基本查询文档
//从es中查询数据
@Test
public void search() {
//数据查询
GetResponse response = client.prepareGet("lib3", "user", "1").execute().actionGet();
//得到查询到的数据
System.out.println(response.getSourceAsString());
client.close();
}
2.添加文档
/**
* 添加文档
*/
@Test
public void insert() throws IOException {
//添加文档
XContentBuilder doc = XContentFactory.jsonBuilder()
.startObject()
.field("id", "1")
.field("title", "java基础")
.field("content", "java是面向对象的语言")
.field("postdate", "2018-05-20")
.field("url", "baidu.com")
.endObject();
//添加文档
IndexResponse response = client.prepareIndex("index1", "blog", "10")
.setSource(doc).get();
System.out.println(response.status());
}
3.删除文档
/**
* 删除文档
*/
@Test
public void delete() {
// 删除文档
DeleteResponse response = client.prepareDelete("index1", "blog", "10").get();
System.out.println(response.status());
}
4.更新文档
@Test
public void update() throws IOException, ExecutionException, InterruptedException {
UpdateRequest request = new UpdateRequest();
request.index("index1")
.type("blog")
.id("10")
.doc(
XContentFactory.jsonBuilder().startObject()
.field("title", "C语言")
.endObject()
);
UpdateResponse response = client.update(request).get();
System.out.println(response.status());
}
4.upsert
@Test
public void upsert() throws IOException, ExecutionException, InterruptedException {
IndexRequest request = new IndexRequest("index1", "blog", "8")
.source(
XContentFactory.jsonBuilder().startObject()
.field("id", "2")
.field("title", "python基础")
.field("content", "python 爬虫")
.field("postdate", "2018-05-20")
.field("url", "baidu.com")
.endObject()
);
UpdateRequest request2 = new UpdateRequest("index1", "blog", "8")
.doc(
XContentFactory.jsonBuilder().startObject()
.field("id", "3")
.field("title", "ruby基础")
.field("content", "ruby")
.field("postdate", "2018-05-20")
.field("url", "baidu.com")
.endObject()
).upsert(request);
UpdateResponse response = client.update(request2).get();
System.out.println(response.status());
}
5.mget批量查询
//mget批量查询
@Test
public void test() {
MultiGetResponse responses = client.prepareMultiGet()
.add("index1", "blog", "8", "10")
.add("lib3", "user", "1", "2", "3")
.get();
for (MultiGetItemResponse item : responses) {
GetResponse gr = item.getResponse();
if (gr != null && gr.isExists()) {
System.out.println(gr.getSourceAsString());
}
}
}
6.bulk批量操作
@Test
public void bulk() throws IOException {
BulkRequestBuilder bulkBuilder = client.prepareBulk();
//批量添加
bulkBuilder.add(
client.prepareIndex("lib2", "books", "8")
.setSource(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "python")
.field("price", 99)
.endObject()
)
);
bulkBuilder.add(
client.prepareIndex("lib2", "books", "9")
.setSource(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "R")
.field("price", 89)
.endObject()
)
);
BulkResponse responses = bulkBuilder.get();
System.out.println(responses.status());
}
7.批量删除
/**
* 删除所有
*/
@Test
public void deleteAll() {
BulkByScrollResponse response = DeleteByQueryAction.INSTANCE
.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("title", "基础"))
.source("index1")
.get();
System.out.println(response.getDeleted());
}
8.查询所有
/**
* 查询所有
*/
@Test
public void selectAll() {
QueryBuilder gb = QueryBuilders.matchAllQuery();
SearchResponse sr = client.prepareSearch("lib3")
.setQuery(gb)
.setSize(3)
.get();
SearchHits hits = sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}