|
|
package com.yoho.core.index.service.impl;
|
|
|
|
|
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
|
|
|
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
|
|
|
import org.elasticsearch.action.bulk.BulkItemResponse;
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.action.update.UpdateAction;
|
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
|
import org.elasticsearch.action.update.UpdateRequestBuilder;
|
|
|
import org.elasticsearch.action.update.UpdateResponse;
|
|
|
import org.elasticsearch.client.Requests;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
/**
|
|
|
* Created by apple on 16/8/6.
|
|
|
*/
|
|
|
public class EsTest {
|
|
|
private TransportClient client;
|
|
|
|
|
|
@Before
|
|
|
public void createEsClient() throws UnknownHostException {
|
|
|
Settings settings = Settings.settingsBuilder()
|
|
|
// .put("cluster.name", "yohosearch111")
|
|
|
.put("cluster.name", "yohosearch_zhurui").put("client.transport.sniff", true).build();
|
|
|
client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void deleteMyStroe() {
|
|
|
DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete("yoho_productindex_1470727824964_20160809153024964").get();
|
|
|
System.out.println(deleteIndexResponse.getHeaders());
|
|
|
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void queryAllIndex() {
|
|
|
ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
|
|
|
String[] indexs = response.getState().getMetaData().getConcreteAllIndices();
|
|
|
for (String index : indexs) {
|
|
|
System.out.println(index);//
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void createMappingTest() {
|
|
|
CreateIndexResponse rssp = client
|
|
|
.admin()
|
|
|
.indices()
|
|
|
.prepareCreate("my_store")
|
|
|
.addMapping(
|
|
|
"products",
|
|
|
"{\n" + " \"products\": {\n" + " \"properties\": {\n" + " \"productID\": {\n" + " \"type\": \"string\",\n"
|
|
|
+ " \"index\": \"not_analyzed\"\n" + " }\n" + " }\n" + " }\n" + " }").get();
|
|
|
System.out.println(rssp.getHeaders());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void bulktest() throws IOException {
|
|
|
BulkRequestBuilder bulkRequest = client.prepareBulk();
|
|
|
bulkRequest.add(client.prepareIndex("my_store1", "products", "1").setSource(
|
|
|
jsonBuilder().startObject().field("price", 10).field("productID", "XHDK-A-1293-#fJ3").endObject()));
|
|
|
bulkRequest.add(client.prepareIndex("my_store2", "products", "2").setSource(
|
|
|
jsonBuilder().startObject().field("price", 20).field("productID", "KDKE-B-9947-#kL5").endObject()));
|
|
|
bulkRequest.add(client.prepareIndex("my_store3", "products1", "3").setSource(
|
|
|
jsonBuilder().startObject().field("price", 30).field("productID", "JODL-X-1937-#pV7").endObject()));
|
|
|
bulkRequest.add(client.prepareIndex("my_store", "products", "4").setSource(
|
|
|
jsonBuilder().startObject().field("price", 40).field("productID", "QQPX-R-3956-#aD8").endObject()));
|
|
|
BulkResponse bulkResponse = bulkRequest.get();
|
|
|
if (bulkResponse.hasFailures()) {
|
|
|
System.out.println("failure");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void queryFilterTerm() {
|
|
|
QueryBuilder queryBuilder = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
|
|
|
System.out.println(s.toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void basequerytest() {
|
|
|
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
|
|
|
System.out.println(s.toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void basequeryAndFiltertest() {
|
|
|
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(filterBuilder).get();
|
|
|
System.out.println(s.toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void filterQueryTest() {
|
|
|
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
|
|
|
// searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
|
|
|
SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(query).get();
|
|
|
System.out.println(s.toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void SearchSourceBuilderTEst() throws ExecutionException, InterruptedException {
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
|
|
|
searchSourceBuilder.query(query);
|
|
|
// searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
|
|
|
SearchRequest request = Requests.searchRequest("my_store").types("products");
|
|
|
request.source(searchSourceBuilder.toString());
|
|
|
System.out.println(searchSourceBuilder.toString());
|
|
|
// System.out.println(client.search(request).get().toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void boolQueryTest() throws ExecutionException, InterruptedException {
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
QueryBuilder t1 = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
QueryBuilder t2 = QueryBuilders.termQuery("price", 10);
|
|
|
QueryBuilder t4 = QueryBuilders.termQuery("price", 30);
|
|
|
searchSourceBuilder.query(QueryBuilders.boolQuery().should(t1).should(t2).mustNot(t4));
|
|
|
System.out.println(searchSourceBuilder);
|
|
|
SearchRequest request = Requests.searchRequest("my_store").types("products");
|
|
|
request.source(searchSourceBuilder.toString());
|
|
|
System.out.println(client.search(request).get().toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void updateDocTest() throws IOException, ExecutionException, InterruptedException {
|
|
|
GetResponse rs = client.prepareGet("my_store", "products", "1").get();
|
|
|
System.out.println("before:" + rs.getSourceAsString());
|
|
|
UpdateRequest updateRequest = new UpdateRequest("my_store", "products", "1").doc(jsonBuilder().startObject().field("price", 12).endObject());
|
|
|
client.update(updateRequest).get();
|
|
|
System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
|
|
|
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void UpdateRequestBuilderTest() throws IOException {
|
|
|
GetResponse rs = client.prepareGet("my_store", "products", "1").get();
|
|
|
System.out.println("before:" + rs.getSourceAsString());
|
|
|
UpdateRequestBuilder request = new UpdateRequestBuilder(client, UpdateAction.INSTANCE, "my_store", "products", "1");
|
|
|
UpdateResponse re = request.setDoc(jsonBuilder().startObject().field("price", 12).endObject()).get();
|
|
|
System.out.println(re.toString());
|
|
|
System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void bulkUpdateTest() throws ExecutionException, InterruptedException {
|
|
|
final BulkRequest request = Requests.bulkRequest();
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("id", i);
|
|
|
map.put("productId", i);
|
|
|
request.add(Requests.indexRequest("productindex").type("productindex").id(i + "").source(map));
|
|
|
if (i % 2 == 0) {
|
|
|
request.add(Requests.deleteRequest("productindex").type("productindex").id(i + ""));
|
|
|
}
|
|
|
}
|
|
|
client.bulk(request).get();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void bulkdeleteTest() throws ExecutionException, InterruptedException {
|
|
|
final BulkRequest request = Requests.bulkRequest();
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
request.add(Requests.deleteRequest("my_index").type("zhurui").id(i + ""));
|
|
|
}
|
|
|
client.bulk(request).get();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void bulkaddTest() throws ExecutionException, InterruptedException {
|
|
|
final BulkRequest request = Requests.bulkRequest();
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("id", i);
|
|
|
map.put("productId", i);
|
|
|
map.put("productName", "zhurui");
|
|
|
map.put("nickname", "markeloff" + i);
|
|
|
request.add(Requests.indexRequest("my_index").type("test").id(i + "").source(map));
|
|
|
}
|
|
|
Iterator<BulkItemResponse> iterator = client.bulk(request).get().iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
BulkItemResponse bulkItemResponse = iterator.next();
|
|
|
System.out.println(bulkItemResponse.getFailureMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |