1
|
-package com.yoho.core.index.service.impl;
|
|
|
2
|
-
|
|
|
3
|
-import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
4
|
-
|
|
|
5
|
-import java.io.IOException;
|
|
|
6
|
-import java.net.InetAddress;
|
|
|
7
|
-import java.net.UnknownHostException;
|
|
|
8
|
-import java.util.HashMap;
|
|
|
9
|
-import java.util.Iterator;
|
|
|
10
|
-import java.util.Map;
|
|
|
11
|
-import java.util.concurrent.ExecutionException;
|
|
|
12
|
-
|
|
|
13
|
-import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
|
|
|
14
|
-import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
|
|
15
|
-import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
|
|
|
16
|
-import org.elasticsearch.action.bulk.BulkItemResponse;
|
|
|
17
|
-import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
18
|
-import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
|
|
19
|
-import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
20
|
-import org.elasticsearch.action.get.GetResponse;
|
|
|
21
|
-import org.elasticsearch.action.search.SearchRequest;
|
|
|
22
|
-import org.elasticsearch.action.search.SearchResponse;
|
|
|
23
|
-import org.elasticsearch.action.update.UpdateAction;
|
|
|
24
|
-import org.elasticsearch.action.update.UpdateRequest;
|
|
|
25
|
-import org.elasticsearch.action.update.UpdateRequestBuilder;
|
|
|
26
|
-import org.elasticsearch.action.update.UpdateResponse;
|
|
|
27
|
-import org.elasticsearch.client.Requests;
|
|
|
28
|
-import org.elasticsearch.client.transport.TransportClient;
|
|
|
29
|
-import org.elasticsearch.common.settings.Settings;
|
|
|
30
|
-import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
|
|
31
|
-import org.elasticsearch.index.query.QueryBuilder;
|
|
|
32
|
-import org.elasticsearch.index.query.QueryBuilders;
|
|
|
33
|
-import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
34
|
-import org.junit.Before;
|
|
|
35
|
-import org.junit.Test;
|
|
|
36
|
-
|
|
|
37
|
-/**
|
|
|
38
|
- * Created by apple on 16/8/6.
|
|
|
39
|
- */
|
|
|
40
|
-public class EsTest {
|
|
|
41
|
- private TransportClient client;
|
|
|
42
|
-
|
|
|
43
|
- @Before
|
|
|
44
|
- public void createEsClient() throws UnknownHostException {
|
|
|
45
|
- Settings settings = Settings.settingsBuilder()
|
|
|
46
|
- // .put("cluster.name", "yohosearch111")
|
|
|
47
|
- .put("cluster.name", "yohosearch_zhurui").put("client.transport.sniff", true).build();
|
|
|
48
|
- client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
|
|
|
49
|
- }
|
|
|
50
|
-
|
|
|
51
|
- @Test
|
|
|
52
|
- public void deleteMyStroe() {
|
|
|
53
|
- DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete("yoho_productindex_1470727824964_20160809153024964").get();
|
|
|
54
|
- System.out.println(deleteIndexResponse.getHeaders());
|
|
|
55
|
-
|
|
|
56
|
- }
|
|
|
57
|
-
|
|
|
58
|
- @Test
|
|
|
59
|
- public void queryAllIndex() {
|
|
|
60
|
- ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
|
|
|
61
|
- String[] indexs = response.getState().getMetaData().getConcreteAllIndices();
|
|
|
62
|
- for (String index : indexs) {
|
|
|
63
|
- System.out.println(index);//
|
|
|
64
|
- }
|
|
|
65
|
- }
|
|
|
66
|
-
|
|
|
67
|
- @Test
|
|
|
68
|
- public void createMappingTest() {
|
|
|
69
|
- CreateIndexResponse rssp = client
|
|
|
70
|
- .admin()
|
|
|
71
|
- .indices()
|
|
|
72
|
- .prepareCreate("my_store")
|
|
|
73
|
- .addMapping(
|
|
|
74
|
- "products",
|
|
|
75
|
- "{\n" + " \"products\": {\n" + " \"properties\": {\n" + " \"productID\": {\n" + " \"type\": \"string\",\n"
|
|
|
76
|
- + " \"index\": \"not_analyzed\"\n" + " }\n" + " }\n" + " }\n" + " }").get();
|
|
|
77
|
- System.out.println(rssp.getHeaders());
|
|
|
78
|
- }
|
|
|
79
|
-
|
|
|
80
|
- @Test
|
|
|
81
|
- public void bulktest() throws IOException {
|
|
|
82
|
- BulkRequestBuilder bulkRequest = client.prepareBulk();
|
|
|
83
|
- bulkRequest.add(client.prepareIndex("my_store1", "products", "1").setSource(
|
|
|
84
|
- jsonBuilder().startObject().field("price", 10).field("productID", "XHDK-A-1293-#fJ3").endObject()));
|
|
|
85
|
- bulkRequest.add(client.prepareIndex("my_store2", "products", "2").setSource(
|
|
|
86
|
- jsonBuilder().startObject().field("price", 20).field("productID", "KDKE-B-9947-#kL5").endObject()));
|
|
|
87
|
- bulkRequest.add(client.prepareIndex("my_store3", "products1", "3").setSource(
|
|
|
88
|
- jsonBuilder().startObject().field("price", 30).field("productID", "JODL-X-1937-#pV7").endObject()));
|
|
|
89
|
- bulkRequest.add(client.prepareIndex("my_store", "products", "4").setSource(
|
|
|
90
|
- jsonBuilder().startObject().field("price", 40).field("productID", "QQPX-R-3956-#aD8").endObject()));
|
|
|
91
|
- BulkResponse bulkResponse = bulkRequest.get();
|
|
|
92
|
- if (bulkResponse.hasFailures()) {
|
|
|
93
|
- System.out.println("failure");
|
|
|
94
|
- }
|
|
|
95
|
- }
|
|
|
96
|
-
|
|
|
97
|
- @Test
|
|
|
98
|
- public void queryFilterTerm() {
|
|
|
99
|
- QueryBuilder queryBuilder = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
100
|
- SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
|
|
|
101
|
- System.out.println(s.toString());
|
|
|
102
|
- }
|
|
|
103
|
-
|
|
|
104
|
- @Test
|
|
|
105
|
- public void basequerytest() {
|
|
|
106
|
- QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
107
|
- SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(queryBuilder).get();
|
|
|
108
|
- System.out.println(s.toString());
|
|
|
109
|
- }
|
|
|
110
|
-
|
|
|
111
|
- @Test
|
|
|
112
|
- public void basequeryAndFiltertest() {
|
|
|
113
|
- QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
114
|
- SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(filterBuilder).get();
|
|
|
115
|
- System.out.println(s.toString());
|
|
|
116
|
- }
|
|
|
117
|
-
|
|
|
118
|
- @Test
|
|
|
119
|
- public void filterQueryTest() {
|
|
|
120
|
- QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
121
|
- QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
122
|
- QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
|
|
|
123
|
- // searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
|
|
|
124
|
- SearchResponse s = client.prepareSearch("my_store").setTypes("products").setQuery(query).get();
|
|
|
125
|
- System.out.println(s.toString());
|
|
|
126
|
- }
|
|
|
127
|
-
|
|
|
128
|
- @Test
|
|
|
129
|
- public void SearchSourceBuilderTEst() throws ExecutionException, InterruptedException {
|
|
|
130
|
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
131
|
- QueryBuilder queryBuilder = QueryBuilders.matchQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
132
|
- QueryBuilder filterBuilder = QueryBuilders.termQuery("price", 10);
|
|
|
133
|
- QueryBuilder query = QueryBuilders.boolQuery().must(queryBuilder).filter(filterBuilder);
|
|
|
134
|
- searchSourceBuilder.query(query);
|
|
|
135
|
- // searchSourceBuilder.postFilter(filterBuilder); 后置过滤器
|
|
|
136
|
- SearchRequest request = Requests.searchRequest("my_store").types("products");
|
|
|
137
|
- request.source(searchSourceBuilder.toString());
|
|
|
138
|
- System.out.println(searchSourceBuilder.toString());
|
|
|
139
|
- // System.out.println(client.search(request).get().toString());
|
|
|
140
|
- }
|
|
|
141
|
-
|
|
|
142
|
- @Test
|
|
|
143
|
- public void boolQueryTest() throws ExecutionException, InterruptedException {
|
|
|
144
|
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
145
|
- QueryBuilder t1 = QueryBuilders.termQuery("productID", "XHDK-A-1293-#fJ3");
|
|
|
146
|
- QueryBuilder t2 = QueryBuilders.termQuery("price", 10);
|
|
|
147
|
- QueryBuilder t4 = QueryBuilders.termQuery("price", 30);
|
|
|
148
|
- searchSourceBuilder.query(QueryBuilders.boolQuery().should(t1).should(t2).mustNot(t4));
|
|
|
149
|
- System.out.println(searchSourceBuilder);
|
|
|
150
|
- SearchRequest request = Requests.searchRequest("my_store").types("products");
|
|
|
151
|
- request.source(searchSourceBuilder.toString());
|
|
|
152
|
- System.out.println(client.search(request).get().toString());
|
|
|
153
|
- }
|
|
|
154
|
-
|
|
|
155
|
- @Test
|
|
|
156
|
- public void updateDocTest() throws IOException, ExecutionException, InterruptedException {
|
|
|
157
|
- GetResponse rs = client.prepareGet("my_store", "products", "1").get();
|
|
|
158
|
- System.out.println("before:" + rs.getSourceAsString());
|
|
|
159
|
- UpdateRequest updateRequest = new UpdateRequest("my_store", "products", "1").doc(jsonBuilder().startObject().field("price", 12).endObject());
|
|
|
160
|
- client.update(updateRequest).get();
|
|
|
161
|
- System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
|
|
|
162
|
-
|
|
|
163
|
- }
|
|
|
164
|
-
|
|
|
165
|
- @Test
|
|
|
166
|
- public void UpdateRequestBuilderTest() throws IOException {
|
|
|
167
|
- GetResponse rs = client.prepareGet("my_store", "products", "1").get();
|
|
|
168
|
- System.out.println("before:" + rs.getSourceAsString());
|
|
|
169
|
- UpdateRequestBuilder request = new UpdateRequestBuilder(client, UpdateAction.INSTANCE, "my_store", "products", "1");
|
|
|
170
|
- UpdateResponse re = request.setDoc(jsonBuilder().startObject().field("price", 12).endObject()).get();
|
|
|
171
|
- System.out.println(re.toString());
|
|
|
172
|
- System.out.println("after" + client.prepareGet("my_store", "products", "1").get().getSourceAsString());
|
|
|
173
|
- }
|
|
|
174
|
-
|
|
|
175
|
- @Test
|
|
|
176
|
- public void bulkUpdateTest() throws ExecutionException, InterruptedException {
|
|
|
177
|
- final BulkRequest request = Requests.bulkRequest();
|
|
|
178
|
- for (int i = 0; i < 10; i++) {
|
|
|
179
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
180
|
- map.put("id", i);
|
|
|
181
|
- map.put("productId", i);
|
|
|
182
|
- request.add(Requests.indexRequest("productindex").type("productindex").id(i + "").source(map));
|
|
|
183
|
- if (i % 2 == 0) {
|
|
|
184
|
- request.add(Requests.deleteRequest("productindex").type("productindex").id(i + ""));
|
|
|
185
|
- }
|
|
|
186
|
- }
|
|
|
187
|
- client.bulk(request).get();
|
|
|
188
|
- }
|
|
|
189
|
-
|
|
|
190
|
- @Test
|
|
|
191
|
- public void bulkdeleteTest() throws ExecutionException, InterruptedException {
|
|
|
192
|
- final BulkRequest request = Requests.bulkRequest();
|
|
|
193
|
- for (int i = 0; i < 10; i++) {
|
|
|
194
|
- request.add(Requests.deleteRequest("my_index").type("zhurui").id(i + ""));
|
|
|
195
|
- }
|
|
|
196
|
- client.bulk(request).get();
|
|
|
197
|
- }
|
|
|
198
|
-
|
|
|
199
|
- @Test
|
|
|
200
|
- public void bulkaddTest() throws ExecutionException, InterruptedException {
|
|
|
201
|
- final BulkRequest request = Requests.bulkRequest();
|
|
|
202
|
- for (int i = 0; i < 10; i++) {
|
|
|
203
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
204
|
- map.put("id", i);
|
|
|
205
|
- map.put("productId", i);
|
|
|
206
|
- map.put("productName", "zhurui");
|
|
|
207
|
- map.put("nickname", "markeloff" + i);
|
|
|
208
|
- request.add(Requests.indexRequest("my_index").type("test").id(i + "").source(map));
|
|
|
209
|
- }
|
|
|
210
|
- Iterator<BulkItemResponse> iterator = client.bulk(request).get().iterator();
|
|
|
211
|
- while (iterator.hasNext()) {
|
|
|
212
|
- BulkItemResponse bulkItemResponse = iterator.next();
|
|
|
213
|
- System.out.println(bulkItemResponse.getFailureMessage());
|
|
|
214
|
- }
|
|
|
215
|
- }
|
|
|
216
|
-
|
|
|
217
|
-} |
|
|