修改配置文件
https://www.elastic.co/guide/cn/elasticsearch/guide/cn/important-configuration-changes.html
修改安装目录>config>elasticsearch.yml文件
新建maven项目,添加TransportClient客户端依赖
version和你下载的版本保持一致
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.2</version>
</dependency>
Demo
package com.example;
import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.stream.IntStream;
public class EsDemo {
public final static String HOST = "127.0.0.1";
public final static int PORT = 9300; //http请求的端口是9200,客户端是9300
public Settings settings;
public TransportClient client;
@Before
public void before() throws UnknownHostException {
// 1.0 指定ElasticSearch集群
settings = Settings.builder().put("cluster.name", "my-application").build();
// 2.0 创建ElasticSearch服务器的客户端
client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(HOST), PORT));
}
@After
public void after() {
// 关闭客户端
client.close();
}
/**
* 查询文档
*
* @throws UnknownHostException
*/
@Test
public void search() {
/**
* 数据查询
* 参数1:index
* 参数2:type
* 参数3:id
*/
GetResponse response = client.prepareGet("article", "poems", "1").execute().actionGet();
System.out.println(response.getSourceAsString());
}
/**
* 添加文档
*
* @throws IOException
*/
@Test
public void add() throws IOException {
String id = StringUtils.toString(System.currentTimeMillis());
// 添加文档
XContentBuilder doc = XContentFactory.jsonBuilder()
.startObject()
.field("title", "春晓")
.field("author", "孟浩然")
.field("dynasty", "唐")
.field("words", 20)
.field("tags", Arrays.asList("雨", "鸟", "五言绝句", "春天"))
.field("content", "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。")
.endObject();
IndexResponse response = client.prepareIndex("article", "poems", id)
.setSource(doc).get();
System.out.println(response.status());
}
/**
* 删除文档
*
* @throws UnknownHostException
*/
@Test
public void delete() {
// 删除文档
DeleteResponse response = client.prepareDelete("article", "poems", "1").get();
System.out.println(response.status());
}
/**
* 更新文档
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void update() throws IOException, InterruptedException, ExecutionException {
// 更新文档
UpdateRequest request = new UpdateRequest();
request.index("article");
request.type("poems");
request.id("1");
request.doc(
XContentFactory.jsonBuilder()
.startObject()
.field("content", "内容被我修改了")
.endObject()
);
UpdateResponse response = client.update(request).get();
System.out.println(response.status());
}
/**
* 文档存在则更新,不存在则新增
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void upsert() throws IOException, InterruptedException, ExecutionException {
XContentBuilder doc = XContentFactory.jsonBuilder()
.startObject()
.field("title", "春晓")
.field("author", "孟浩然")
.field("dynasty", "唐")
.field("words", 20)
.field("tags", Arrays.asList("雨", "鸟", "五言绝句", "春天"))
.field("content", "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。")
.endObject();
IndexRequest request1 = new IndexRequest("article", "poems", "1").source(doc);
// 更新文档
UpdateRequest request2 = new UpdateRequest("article", "poems", "1")
.doc(
XContentFactory.jsonBuilder()
.startObject()
.field("content", "内容被我修改了2")
.endObject()
)
.upsert(request1);
UpdateResponse response = client.update(request2).get();
System.out.println(response.status());
}
/**
* bulk批量操作
*
* @throws IOException
*/
@Test
public void bulk() throws IOException {
BulkRequestBuilder bulkBuild = client.prepareBulk();
bulkBuild.add(client.prepareUpdate("article", "poems", "1")
.setDoc(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "批量修改标题1")
.field("content", "批量修改内容1")
.endObject()
)
);
bulkBuild.add(client.prepareUpdate("article", "poems", "2")
.setDoc(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "批量修改标题2")
.field("content", "批量修改内容2")
.endObject()
)
);
bulkBuild.add(client.prepareIndex("article", "poems", System.currentTimeMillis() + "")
.setSource(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "批量新增标题1")
.field("content", "批量新增内容1")
.endObject()
)
);
bulkBuild.add(client.prepareDelete("article", "poems", "1"));
BulkResponse response = bulkBuild.get();
System.out.println(response.status());
if (response.hasFailures()) {
System.out.println("失败了");
}
}
/**
* 查询出来并删除
*
* @throws IOException
*/
@Test
public void searchAndDelete() throws IOException {
BulkByScrollResponse response = DeleteByQueryAction.INSTANCE
.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("title", "批量"))
.source("article")
.get();
long count = response.getDeleted();
System.out.println("删除文档的数量:" + count);
}
/**
* 各种查询方式
*
* @throws IOException
*/
@Test
public void query() throws IOException {
// match查询
// 查询所有
// QueryBuilder qb = QueryBuilders.matchAllQuery();
/**
* 参数1:field
* 参数2:搜索内容
*/
// QueryBuilder qb = QueryBuilders.matchQuery("title", "春");
/**
* 参数1:搜索内容
* 参数2...:field, field, field...
*/
// QueryBuilder qb = QueryBuilders.multiMatchQuery("春", "title", "content");
// term查询
/**
* 参数1:field
* 参数2:搜索内容
*/
// QueryBuilder qb = QueryBuilders.termQuery("title", "静");
// QueryBuilder qb = QueryBuilders.termsQuery("title", "静", "春");
// 范围查询
// QueryBuilder qb = QueryBuilders.rangeQuery("words").from(10).to(30);
// 前缀查询
// QueryBuilder qb = QueryBuilders.prefixQuery("author", "李");
// 通配符查询
// QueryBuilder qb = QueryBuilders.wildcardQuery("author", "李*");
// 指定多个id查询
// QueryBuilder qb = QueryBuilders.idsQuery().addIds("1", "2");
// query string
// QueryBuilder qb = QueryBuilders.queryStringQuery("+java -c");
// 组合查询,查询姓李的写的多于20字的诗
BoolQueryBuilder qb = QueryBuilders.boolQuery()
.must(QueryBuilders.prefixQuery("author", "李"))
.mustNot(QueryBuilders.rangeQuery("words").to("20"));
SearchResponse response = client.prepareSearch("article")
.setQuery(qb)
.setSize(10)
.get();
for (SearchHit hit : response.getHits().getHits()){
System.out.println(hit.getSourceAsMap());
}
}
/**
* 聚合查询
*
* @throws IOException
*/
@Test
public void aggregation() throws IOException {
//字数最多的诗有多少字
AggregationBuilder agg = AggregationBuilders.max("aggMax").field("words");
SearchResponse response = client.prepareSearch("article").addAggregation(agg).get();
Max max = response.getAggregations().get("aggMax");
System.out.println(max.getValue());
}
/**
* 滚动查询
*/
@Test
public void addTestData(){
//先插入十万条测试数据
int count = 100000;
Long startTime = System.currentTimeMillis();
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
IntStream.range(1, count+1).forEach(i -> {
try {
bulkRequestBuilder.add(client.prepareIndex("test", "testData")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("id", i)
.field("name", "xiaoming-"+i)
.endObject()
)
);
} catch (IOException e) {
e.printStackTrace();
}
});
BulkResponse responses = bulkRequestBuilder.execute().actionGet();
System.out.println("插入"+count+"条测试数据,耗时"+(System.currentTimeMillis()-startTime)+"毫秒");
}
@Test
public void scrollSearch(){
SearchRequestBuilder requestBuilder = client.prepareSearch("test").setTypes("testData");
SearchResponse response = requestBuilder.setQuery(QueryBuilders.matchAllQuery()).setScroll(new TimeValue(1000)).setSize(10).addSort("id", SortOrder.ASC).get();
do {
for (SearchHit hit : response.getHits().getHits()){
System.out.println(hit.getSourceAsMap());
}
response = client.prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(1000)).get();
}while (response.getHits().getHits().length != 0);
ClearScrollResponse response2 = client.prepareClearScroll().addScrollId(response.getScrollId()).get();
}
}