服务器之家

服务器之家 > 正文

详解spring中使用Elasticsearch的代码实现

时间:2020-10-28 15:03     来源/作者:周游列国之仕子

在使用Elasticsearch之前,先给大家聊一点干货。

1.      ES和solr都是作为全文搜索引擎出现的。都是基于Lucene的搜索服务器。

2.   ES不是可靠的存储系统,不是数据库,它有丢数据的风险。

3.  ES不是实时系统,数据写入成功只是trans log成功(类似于MySQL的bin log),写入成功后立刻查询查不到是正常的。因为数据此刻可能还在内存里而不是进入存储引擎里。同理,删除一条数据后也不是马上消失。写入何时可查询?ES内部有一个后台线程,定时将内存中的一批数据写入到存储引擎,此后数据可见。默认后台线程一秒运行一次。该线程运行的越频繁,写入性能越低。运行的频率越低,写入的性能越高(不会无限高)。

4.    目前已知的单ES集群可以存储PB级别的数据,不过这个就非常费劲了。TB级别数据没压力。

5.    如果使用ES官方提供的jar包访问,需要JDK1.7及以上。

6.    使用对应的版本访问ES server。如果ES server端的版本是1.7,那么请使用ES 1.7的client。如果ES server是2.1,请使用2.1的client。

7.     ES索引存在Linux服务器的文件系统之上(背后是文件系统,不是类似于HDFS的分布式文件系统)

8.     ES Java client是线程安全的,全局构建一个即可满足读写需求,不要每次都创建ES client。每次访问ES都构建新的es client即会抛出次异常。

9.    非常不建议使用ES的动态识别和创建的机制,因为很多情况下这并非你所需要。推荐的做法是在写数据之前仔细的创建mapping。

10.   强烈不建议在ES中使用深分页。可能会导致集群不可用。

11.    ES是静态分片,一旦分片数在创建索引时确定那么后继不能修改。

12.    ES里提供了type,很多人以为type是物理表,一个type的数据是独立存储的;但是在ES内部并不是这样,type在ES内部仅仅是一个字段。所以在很多数据能分为独立index的情况下,不要放到一个index里用type去分。只有嵌套类和父子类的情况下使用type才是合理的。

13.    ES并不提供原生的中文分词的能力。有第三方的中文分词的插件,比如ik等。Ik是个toy分词器,有严肃的分词需求的话,请在使用ES之前使用独立的分词器分好词后向ES写入。

14.    ES中的index,首先会进行分片,每一个分片数据一般都会有自己的副本数据,ES分配分片的策略会保证同一个分片数据和自己的副本不会分配到同一个节点上。当集群中的某一节点宕机后,ES的master在ping该节点时通过一定的策略会发现该节点不存活;会开启ES的恢复过程

15.    ES没有update的能力。所有的update都是标记删除老文档,然后重新insert一条新文档。

好了,回归正题。

首先:

增加我们的spring配置

?
1
2
3
4
5
6
<bean id="client" factory-bean="esClientBuilder" factory-method="init" destroy-method="close"/>
 
  <bean id="esClientBuilder" class="com.***.EsClientBuilder">
    <property name="clusterName" value="集群名称" />
    <property name="nodeIpInfo" value="集群地址" />
  </bean>

其次:

编写我们的EsClientBuilder类初始化我们的ES参数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ***;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class EsClientBuilder {
 
 
  private String clusterName;
  private String nodeIpInfo;
  private TransportClient client;
 
  public Client init(){
    //设置集群的名字
    Settings settings = Settings.settingsBuilder()
        .put("client.transport.sniff", false)
        .put("cluster.name", clusterName)
        .build();
    //创建集群client并添加集群节点地址
    client = TransportClient.builder().settings(settings).build();
    Map<String, Integer> nodeMap = parseNodeIpInfo();
    for (Map.Entry<String,Integer> entry : nodeMap.entrySet()){
      try {
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(entry.getKey()), entry.getValue()));
      } catch (UnknownHostException e) {
        e.printStackTrace();
      }
    }
 
    return client;
  }
 
  /**
   * 解析节点IP信息,多个节点用逗号隔开,IP和端口用冒号隔开
   *
   * @return
   */
  private Map<String, Integer> parseNodeIpInfo(){
    String[] nodeIpInfoArr = nodeIpInfo.split(",");
    Map<String, Integer> map = new HashMap<String, Integer>(nodeIpInfoArr.length);
    for (String ipInfo : nodeIpInfoArr){
      String[] ipInfoArr = ipInfo.split(":");
      map.put(ipInfoArr[0], Integer.parseInt(ipInfoArr[1]));
    }
    return map;
  }
 
  public String getClusterName() {
    return clusterName;
  }
 
  public void setClusterName(String clusterName) {
    this.clusterName = clusterName;
  }
 
  public String getNodeIpInfo() {
    return nodeIpInfo;
  }
 
  public void setNodeIpInfo(String nodeIpInfo) {
    this.nodeIpInfo = nodeIpInfo;
  }
}

最后:

下面我们就可以写自己的service类了,此类就可以通过es的原生api来操作我们的es(这里我们展示的2.X版本的)
indexName相当于数据库名,typeName相当于表名

请参考EsServiceImpl.Java文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package ***;
 
@Service("esService")
public class EsServiceImpl{
 
  @Autowired
  private Client client;
 
  /**
   * 用docId获取document
   * @param indexName
   * @param typeName
   * @param docId
   */
  private static void getWithId(String indexName, String typeName, String docId) {
    //get with id
    GetResponse gResponse = client.prepareGet(indexName, typeName, docId).execute().actionGet();
    System.out.println(gResponse.getIndex());
    System.out.println(gResponse.getType());
    System.out.println(gResponse.getVersion());
    System.out.println(gResponse.isExists());
    Map<String, Object> results = gResponse.getSource();
    if(results != null) {
      for(String key : results.keySet()) {
        Object field = results.get(key);
        System.out.println(key);
        System.out.println(field);
      }
    }
  }
  private static void indexWithBulk(String index, String type) {
        //指定索引名称,type名称和documentId(documentId可选,不设置则系统自动生成)创建document
        IndexRequest ir1 = new IndexRequest();
        String source1 = "{" + "\"user\":\"kimchy\"," + "\"price\":\"6.3\"," + "\"tid\":\"20001\"," + "\"message\":\"Elasticsearch\"" + "}";
        ir1.index(index).type(type).id("100").source(source1);
        IndexRequest ir2 = new IndexRequest();
        String source2 = "{" + "\"user\":\"kimchy2\"," + "\"price\":\"7.3\"," + "\"tid\":\"20002\"," + "\"message\":\"Elasticsearch\"" + "}";
        ir2.index(index).type(type).id("102").source(source2);
        IndexRequest ir3 = new IndexRequest();
        String source3 = "{" + "\"user\":\"kimchy3\"," + "\"price\":\"8.3\"," + "\"tid\":\"20003\"," + "\"message\":\"Elasticsearch\"" + "}";
        ir3.index(index).type(type).id("103").source(source3);
        BulkResponse response = client.prepareBulk().add(ir1).add(ir2).add(ir3).execute().actionGet();
        BulkItemResponse[] responses = response.getItems();
        if(responses != null && responses.length > 0) {
          for(BulkItemResponse r : responses) {
            String i = r.getIndex();
            String t = r.getType();
            System.out.println(i+","+t);
          }
        }
     
  }
  private static void sumCountSearch(String indexName, String typeName,
    String sumField, String countField, String searchField, String searchValue) {
    SumBuilder sb = AggregationBuilders.sum("sumPrice").field(sumField);
    TermQueryBuilder tb = QueryBuilders.termQuery(searchField, searchValue);
    SearchResponse sResponse = client.prepareSearch(indexName).setTypes(typeName).setQuery(tb).addAggregation(sb).execute().actionGet();
    Map<String, Aggregation> aggMap = sResponse.getAggregations().asMap();
    if(aggMap != null && aggMap.size() > 0) {
      for(String key : aggMap.keySet()) {
        if("sumPrice".equals(key)) {
          Sum s = (Sum)aggMap.get(key);
          System.out.println(key + "," + s.getValue());  
        }
        else if("countTid".equals(key)) {
          StatsBuilder c = (StatsBuilder)aggMap.get(key);
          System.out.println(key + "," + c.toString());
        }
      }
    }
  }
  private static void updateDoc(String indexName, String typeName, String id) throws IOException, InterruptedException, ExecutionException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(indexName);
    updateRequest.type(typeName);
    updateRequest.id(id);
    updateRequest.doc(jsonBuilder().startObject().field("gender", "male").endObject());
    UpdateResponse resp = client.update(updateRequest).get();
    resp.getClass();
  }
  private static void scrollSearch(String indexName, String typeName, String... ids) {
    IdsQueryBuilder qb = QueryBuilders.idsQuery().addIds(ids);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        .setSearchType(SearchType.SCAN)
        .setQuery(qb)
        .setScroll(new TimeValue(100))
        .setSize(50)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards+","+timeCost+","+sShards);
     
    while (true) {
      SearchHits hits = sResponse.getHits();
      SearchHit[] hitArray = hits.getHits();
      for(int i = 0; i < hitArray.length; i++) {
        SearchHit hit = hitArray[i];
        Map<String, Object> fields = hit.getSource();
        for(String key : fields.keySet()) {
          System.out.println(key);
          System.out.println(fields.get(key));
        }
      }
      sResponse = client.prepareSearchScroll(sResponse.getScrollId()).setScroll(new TimeValue(100)).execute().actionGet();
      if (sResponse.getHits().getHits().length == 0) {
        break;
      }
    }
  }
  private static void deleteDocuments(String string, String string2) {
    SearchResponse sResponse = client.prepareSearch(string)
        .setTypes(string2)
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.matchAllQuery())
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    List<String> ids = new ArrayList<String>(hitArray.length);
    for(int i = 0; i < count; i++) {
      System.out.println("==================================");
      SearchHit hit = hitArray[i];
      ids.add(hit.getId());
       
    }
    for(String id : ids) {
      DeleteResponse response = client.prepareDelete(string, string2, id).execute().actionGet();
    }  
  }
  private static void dateRangeSearch(String indexName, String typeName,
      String termName, String from, String to) {
    // 构建range query
    //2015-08-20 12:27:11
        QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(from).to(to);
        SearchResponse sResponse = client.prepareSearch(indexName)
            .setTypes(typeName)
            // 设置search type
            // 常用search type用:query_then_fetch
            // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            // 查询的termName和termvalue
            .setQuery(qb)
            // 设置排序field
            .addSort(termName, SortOrder.DESC)
            // 设置分页
            .setFrom(0).setSize(60).execute().actionGet();
        int tShards = sResponse.getTotalShards();
        long timeCost = sResponse.getTookInMillis();
        int sShards = sResponse.getSuccessfulShards();
        System.out.println(tShards + "," + timeCost + "," + sShards);
        SearchHits hits = sResponse.getHits();
        long count = hits.getTotalHits();
        SearchHit[] hitArray = hits.getHits();
        for (int i = 0; i < count; i++) {
          SearchHit hit = hitArray[i];
          Map<String, Object> fields = hit.getSource();
          for (String key : fields.keySet()) {
            System.out.println(key);
            System.out.println(fields.get(key));
          }
        }
  }
  private static void dateRangeSearch2(String indexName, String typeName,
      String termName, String from, String to) {
    // 构建range query
      QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(from).to(to);
      SearchResponse sResponse = client.prepareSearch(indexName)
          .setTypes(typeName)
          // 设置search type
          // 常用search type用:query_then_fetch
          // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
          .setSearchType(SearchType.QUERY_THEN_FETCH)
          // 查询的termName和termvalue
          .setQuery(qb)
          // 设置排序field
          .addSort(termName, SortOrder.DESC)
          // 设置分页
          .setFrom(0).setSize(60).execute().actionGet();
      int tShards = sResponse.getTotalShards();
      long timeCost = sResponse.getTookInMillis();
      int sShards = sResponse.getSuccessfulShards();
      System.out.println(tShards + "," + timeCost + "," + sShards);
      SearchHits hits = sResponse.getHits();
      long count = hits.getTotalHits();
      SearchHit[] hitArray = hits.getHits();
      for (int i = 0; i < count; i++) {
        SearchHit hit = hitArray[i];
        Map<String, Object> fields = hit.getSource();
        for (String key : fields.keySet()) {
          System.out.println(key);
          System.out.println(fields.get(key));
        }
      }
  }
  private static void countWithQuery(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) {
    //search result get source
        CountResponse cResponse = client.prepareCount(indexName)
            .setTypes(typeName)
            .setQuery(QueryBuilders.termQuery(termName, termValue))
            .execute()
            .actionGet();
        int tShards = cResponse.getTotalShards();
        int sShards = cResponse.getSuccessfulShards();
        System.out.println(tShards+","+sShards);
        long count = cResponse.getCount();   
  }
  private static void rangeSearchWithOtherSearch(String indexName, String typeName,
      String termName, String min, String max, String termQueryField) {
    // 构建range query
        QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max);
        TermQueryBuilder tb = QueryBuilders.termQuery(termName, termQueryField);
        BoolQueryBuilder bq = boolQuery().must(qb).must(tb);
        SearchResponse sResponse = client.prepareSearch(indexName)
            .setTypes(typeName)
            // 设置search type
            // 常用search type用:query_then_fetch
            // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            // 查询的termName和termvalue
            .setQuery(bq)
            // 设置排序field
            .addSort(termName, SortOrder.DESC)
            // 设置分页
            .setFrom(0).setSize(60).execute().actionGet();
        int tShards = sResponse.getTotalShards();
        long timeCost = sResponse.getTookInMillis();
        int sShards = sResponse.getSuccessfulShards();
        System.out.println(tShards + "," + timeCost + "," + sShards);
        SearchHits hits = sResponse.getHits();
        long count = hits.getTotalHits();
        SearchHit[] hitArray = hits.getHits();
        for (int i = 0; i < count; i++) {
          SearchHit hit = hitArray[i];
          Map<String, Object> fields = hit.getSource();
          for (String key : fields.keySet()) {
            System.out.println(key);
            System.out.println(fields.get(key));
          }
        }
  }
  private static void termRangeSearch(String indexName, String typeName,
    String termName, String min, String max, String highlightField) {
    QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        // 设置search type
        // 常用search type用:query_then_fetch
        // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        // 查询的termName和termvalue
        .setQuery(qb)
        // 设置排序field
        .addSort(termName, SortOrder.DESC)
        //设置高亮field
        .addHighlightedField(highlightField)
        // 设置分页
        .setFrom(0).setSize(60).execute().actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards + "," + timeCost + "," + sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for (int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for (String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
  private static void sumOneField(String indexName, String typeName, String fieldName) {
    SumBuilder sb = AggregationBuilders.sum("sum").field(fieldName);
    //search result get source
    SearchResponse sResponse = client.prepareSearch(indexName).setTypes(typeName).addAggregation(sb).execute().actionGet();
    Map<String, Aggregation> aggMap = sResponse.getAggregations().asMap();
    if(aggMap != null && aggMap.size() > 0) {
      for(String key : aggMap.keySet()) {
        Sum s = (Sum)aggMap.get(key);
        System.out.println(s.getValue());
      }
    }
  }
  private static void searchWithTermQueryAndRetureSpecifiedFields(String indexName, String typeName, String termName,String termValue, String sortField, String highlightField,String... fields) {
     SearchRequestBuilder sb = client.prepareSearch(indexName)
        .setTypes(typeName)
        // 设置search type
        // 常用search type用:query_then_fetch
        // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        // 查询的termName和termvalue
        .setQuery(QueryBuilders.termQuery(termName, termValue))
        // 设置排序field
        .addSort(sortField, SortOrder.DESC)
        // 设置高亮field
        .addHighlightedField(highlightField)
        // 设置分页
        .setFrom(0).setSize(60);
    for (String field : fields) {
      sb.addField(field);
    }
    SearchResponse sResponse = sb.execute().actionGet();
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for (int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, SearchHitField> fm = hit.getFields();
      for (String key : fm.keySet()) {
        SearchHitField f = fm.get(key);
        System.out.println(f.getName());
        System.out.println(f.getValue());
      }
    }
  }
  private static void searchWithIds(String indexName, String typeName, String sortField, String highlightField, String... ids) {
    IdsQueryBuilder qb = QueryBuilders.idsQuery().addIds(ids);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        //设置search type
        //常用search type用:query_then_fetch
        //query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        //查询的termName和termvalue
        .setQuery(qb)
        //设置排序field
        .addSort(sortField, SortOrder.DESC)
        //设置高亮field
        .addHighlightedField(highlightField)
        //设置分页
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards+","+timeCost+","+sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for(int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for(String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
 
  /**
   * 在index:indexName, type:typeName中做通配符查询
   * @param indexName
   * @param typeName
   * @param termName
   * @param termValue
   * @param sortField
   * @param highlightField
   */
  private static void wildcardSearch(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) {
    QueryBuilder qb = QueryBuilders.wildcardQuery(termName, termValue);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        //设置search type
        //常用search type用:query_then_fetch
        //query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        //查询的termName和termvalue
        .setQuery(qb)
        //设置排序field
//       .addSort(sortField, SortOrder.DESC)
        //设置高亮field
//       .addHighlightedField(highlightField)
        //设置分页
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards+","+timeCost+","+sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for(int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for(String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
 
  /**
   * 在index:indexName, type:typeName中做模糊查询
   * @param indexName
   * @param typeName
   * @param termName
   * @param termValue
   * @param sortField
   * @param highlightField
   */
  private static void fuzzySearch(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) {
     QueryBuilder qb = QueryBuilders.fuzzyQuery(termName, termValue);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        //设置search type
        //常用search type用:query_then_fetch
        //query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        //查询的termName和termvalue
        .setQuery(qb)
        //设置排序field
        .addSort(sortField, SortOrder.DESC)
        //设置高亮field
        .addHighlightedField(highlightField)
        //设置分页
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards+","+timeCost+","+sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for(int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for(String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
  /**
   * 在index:indexName, type:typeName中做区间查询
   * @param indexName
   * @param typeName
   * @param termName
   * @param min
   * @param max
   * @param highlightField
   */
  private static void numericRangeSearch(String indexName, String typeName,
      String termName, double min, double max, String highlightField) {
    // 构建range query
    QueryBuilder qb = QueryBuilders.rangeQuery(termName).from(min).to(max);
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        // 设置search type
        // 常用search type用:query_then_fetch
        // query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        // 查询的termName和termvalue
        .setQuery(qb)
        // 设置排序field
        .addSort(termName, SortOrder.DESC)
        //设置高亮field
        .addHighlightedField(highlightField)
        // 设置分页
        .setFrom(0).setSize(60).execute().actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards + "," + timeCost + "," + sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for (int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for (String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
  /**
   * 在索引indexName, type为typeName中查找两个term:term1(termName1, termValue1)和term2(termName2, termValue2)
   * @param indexName
   * @param typeName
   * @param termName1
   * @param termValue1
   * @param termName2
   * @param termValue2
   * @param sortField
   * @param highlightField
   */
  private static void searchWithBooleanQuery(String indexName, String typeName, String termName1, String termValue1, 
       String termName2, String termValue2, String sortField, String highlightField) {
    //构建boolean query
    BoolQueryBuilder bq = boolQuery().must(termQuery(termName1, termValue1)).must(termQuery(termName2, termValue2));  
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        //设置search type
        //常用search type用:query_then_fetch
        //query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        //查询的termName和termvalue
        .setQuery(bq)
        //设置排序field
        .addSort(sortField, SortOrder.DESC)
        //设置高亮field
        .addHighlightedField(highlightField)
        //设置分页
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    System.out.println(tShards+","+timeCost+","+sShards);
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for(int i = 0; i < count; i++) {
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for(String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
  /**
   * 在索引indexName, type为typeName中查找term(termName, termValue)
   * @param indexName
   * @param typeName
   * @param termName
   * @param termValue
   * @param sortField
   * @param highlightField
   */
  private static void searchWithTermQuery(String indexName, String typeName, String termName, String termValue, String sortField, String highlightField) {
    SearchResponse sResponse = client.prepareSearch(indexName)
        .setTypes(typeName)
        //设置search type
        //常用search type用:query_then_fetch
        //query_then_fetch是先查到相关结构,然后聚合不同node上的结果后排序
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        //查询的termName和termvalue
        .setQuery(QueryBuilders.termQuery(termName, termValue))
        //设置排序field
//       .addSort(sortField, SortOrder.DESC)
        //设置高亮field
//       .addHighlightedField(highlightField)
        //设置分页
        .setFrom(0).setSize(60)
        .execute()
        .actionGet();
    int tShards = sResponse.getTotalShards();
    long timeCost = sResponse.getTookInMillis();
    int sShards = sResponse.getSuccessfulShards();
    SearchHits hits = sResponse.getHits();
    long count = hits.getTotalHits();
    SearchHit[] hitArray = hits.getHits();
    for(int i = 0; i < count; i++) {
      System.out.println("==================================");
      SearchHit hit = hitArray[i];
      Map<String, Object> fields = hit.getSource();
      for(String key : fields.keySet()) {
        System.out.println(key);
        System.out.println(fields.get(key));
      }
    }
  }
  /**
   * 用java的map构建document
   */
  private static void indexWithMap(String indexName, String typeName) {
    Map<String, Object> json = new HashMap<String, Object>();
    //设置document的field
    json.put("user","kimchy2");
    json.put("postDate",new Date());
    json.put("price",6.4);
    json.put("message","Elasticsearch");
    json.put("tid","10002");
    json.put("endTime","2015-08-25 09:00:00");
    //指定索引名称,type名称和documentId(documentId可选,不设置则系统自动生成)创建document
    IndexResponse response = client.prepareIndex(indexName, typeName, "2").setSource(json).execute().actionGet();
    //response中返回索引名称,type名称,doc的Id和版本信息
    String index = response.getIndex();
    String type = response.getType();
    String id = response.getId();
    long version = response.getVersion();
    boolean created = response.isCreated();
    System.out.println(index+","+type+","+id+","+version+","+created);
  }
 
  /**
   * 用java字符串创建document
   */
  private static void indexWithStr(String indexName, String typeName) {
    //手工构建json字符串
    //该document包含user, postData和message三个field
    String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"price\":\"6.3\"," + "\"tid\":\"10001\"," + "}";
    //指定索引名称,type名称和documentId(documentId可选,不设置则系统自动生成)创建document
    IndexResponse response = client.prepareIndex(indexName, typeName, "1")
        .setSource(json)
        .execute()
        .actionGet();
    //response中返回索引名称,type名称,doc的Id和版本信息
    String index = response.getIndex();
    String type = response.getType();
    String id = response.getId();
    long version = response.getVersion();
    boolean created = response.isCreated();
    System.out.println(index+","+type+","+id+","+version+","+created);
  }
   
  private static void deleteDocWithId(String indexName, String typeName, String docId) {
    DeleteResponse dResponse = client.prepareDelete(indexName, typeName, docId).execute().actionGet();
    String index = dResponse.getIndex();
    String type = dResponse.getType();
    String id = dResponse.getId();
    long version = dResponse.getVersion();
    System.out.println(index+","+type+","+id+","+version);
  }
   
  /**
   * 创建索引
   * 注意:在生产环节中通知es集群的owner去创建index
   * @param client
   * @param indexName
   * @param documentType
   * @throws IOException
   */
  private static void createIndex(String indexName, String documentType) throws IOException {
    final IndicesExistsResponse iRes = client.admin().indices().prepareExists(indexName).execute().actionGet();
    if (iRes.isExists()) {
      client.admin().indices().prepareDelete(indexName).execute().actionGet();
    }
    client.admin().indices().prepareCreate(indexName).setSettings(Settings.settingsBuilder().put("number_of_shards", 1).put("number_of_replicas", "0")).execute().actionGet();
    XContentBuilder mapping = jsonBuilder()
        .startObject()
           .startObject(documentType)
//           .startObject("_routing").field("path","tid").field("required", "true").endObject()
           .startObject("_source").field("enabled", "true").endObject()
           .startObject("_all").field("enabled", "false").endObject()
             .startObject("properties")
               .startObject("user")
                 .field("store", true)
                 .field("type", "string")
                 .field("index", "not_analyzed")
                .endObject()
                .startObject("message")
                 .field("store", true)
                 .field("type","string")
                 .field("index", "analyzed")
                 .field("analyzer", "standard")
                .endObject()
                .startObject("price")
                 .field("store", true)
                 .field("type", "float")
                .endObject()
                .startObject("nv1")
                 .field("store", true)
                 .field("type", "integer")
                 .field("index", "no")
                 .field("null_value", 0)
                .endObject()
                .startObject("nv2")
                 .field("store", true)
                 .field("type", "integer")
                 .field("index", "not_analyzed")
                 .field("null_value", 10)
                .endObject()
                .startObject("tid")
                 .field("store", true)
                 .field("type", "string")
                 .field("index", "not_analyzed")
                .endObject()
                .startObject("endTime")
                 .field("type", "date")
                 .field("store", true)
                 .field("index", "not_analyzed")
                 .field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                .endObject()
                .startObject("date")
                 .field("type", "date")
                .endObject()
             .endObject()
           .endObject()
          .endObject();
    client.admin().indices()
        .preparePutMapping(indexName)
        .setType(documentType)
        .setSource(mapping)
        .execute().actionGet();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/antao592/article/details/52872854

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部