Elasticsearch5.0 Java Api -- 检索索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch5.0 Java Api -- 检索索引相关的知识,希望对你有一定的参考价值。
测试检索索引库的功能
1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 5 import org.elasticsearch.client.Client; 6 import org.elasticsearch.common.settings.Settings; 7 import org.elasticsearch.common.transport.InetSocketTransportAddress; 8 import org.elasticsearch.index.query.QueryBuilder; 9 import org.elasticsearch.index.query.QueryBuilders; 10 import org.elasticsearch.search.SearchHit; 11 import org.elasticsearch.search.SearchHits; 12 import org.elasticsearch.transport.client.PreBuiltTransportClient; 13 import org.elasticsearch.action.get.GetResponse; 14 import org.elasticsearch.action.get.MultiGetItemResponse; 15 import org.elasticsearch.action.get.MultiGetResponse; 16 import org.elasticsearch.action.search.SearchResponse; 17 18 public class ElasticSearchGet { 19 20 private static Client client; 21 22 /** 23 * 测试检索索引库的功能 24 * @param args 25 */ 26 public static void main(String[] args) { 27 28 try { 29 // 设置集群名称 30 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 31 // 创建client 32 client = new PreBuiltTransportClient(settings) 33 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.10.125"), 9300)); 34 35 36 //multiGet("flow", "data", "AVvnE1sA3HhivpslN8lN"); 37 getAll("newindex","newhttp"); 38 39 40 41 // 关闭client 42 client.close(); 43 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 } 48 49 // 通过单一的ID获取一个文档 50 public static void get(String indexName,String TypeName,String id){ 51 MultiGetResponse multiGetItemResponses = client.prepareMultiGet() 52 .add(indexName, TypeName, id) 53 .get(); 54 55 for (MultiGetItemResponse itemResponse : multiGetItemResponses) { // 遍历结果集 56 GetResponse response = itemResponse.getResponse(); 57 if (response.isExists()) { // 检验文档是否存在 58 String json = response.getSourceAsString(); // 获取文档源 59 System.out.println(json); 60 } 61 } 62 } 63 64 // 传入多个id,从相同的索引名/类型名中获取多个文档 65 public static void multiGet(String indexName,String TypeName,String id1,String id2,String id3){ 66 MultiGetResponse multiGetItemResponses = client.prepareMultiGet() 67 .add(indexName, TypeName, id1, id2, id3) // 传入多个id,从相同的索引名/类型名中获取多个文档 68 .add("another", "type", "foo") // 可以同时获取不同索引中的文档 69 .get(); 70 71 for (MultiGetItemResponse itemResponse : multiGetItemResponses) { // 遍历结果集 72 GetResponse response = itemResponse.getResponse(); 73 if (response.isExists()) { // 检验文档是否存在 74 String json = response.getSourceAsString(); // 获取文档源 75 System.out.println(json); 76 } 77 } 78 } 79 80 // 同时获取不同索引中的文档 81 public static void multiGet(String indexName1,String indexName2,String indexName3){ 82 MultiGetResponse multiGetItemResponses = client.prepareMultiGet() 83 .add(indexName1, indexName2,indexName3) // 可以同时获取不同索引中的文档 84 .get(); 85 86 for (MultiGetItemResponse itemResponse : multiGetItemResponses) { // 遍历结果集 87 GetResponse response = itemResponse.getResponse(); 88 if (response.isExists()) { // 检验文档是否存在 89 String json = response.getSourceAsString(); // 获取文档源 90 System.out.println(json); 91 } 92 } 93 } 94 95 // 根据索引和类别查询所有数据 96 public static void getAll(String indexName,String typeName){ 97 98 QueryBuilder queryBuilder =QueryBuilders.matchAllQuery(); 99 SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(typeName) 100 .setQuery(queryBuilder) 101 .execute() 102 .actionGet(); 103 SearchHits hits = searchResponse.getHits(); // 获取搜索结果 104 System.out.println("查询到记录数:" + hits.getTotalHits()); 105 SearchHit[] searchHists = hits.getHits(); 106 if(searchHists.length>0){ 107 for(SearchHit hit:searchHists){ 108 System.out.println(hit.getSource()); 109 //System.out.println(hit.id()); 110 } 111 } 112 } 113 114 115 116 // todo... 117 // 字段和内容查询(分词功能未加) 118 public static void get(){ 119 120 // 创建QueryBuilder,可以设置单个字段的查询,也可以设置多个字段的查询 121 QueryBuilder qb1 = QueryBuilders.termQuery("time", "2017-05-08T02:50:47.700Z"); // 查询time字段中包含2关键字的文档 122 QueryBuilder qb2= QueryBuilders.multiMatchQuery("2", "time","inbyte"); // 查询time或inbyte字段中包含2关键字的文档 123 124 // 执行查询,通过client设置查询的index、type、query.返回一个SearchResponse对象 125 SearchResponse response = client.prepareSearch("flow").setTypes("data").setQuery(qb1).execute() 126 .actionGet(); 127 // 处理查询结果,获取查询结果集合 128 SearchHits hits = response.getHits(); 129 if (hits.totalHits() > 0) { 130 for (SearchHit hit : hits) { 131 System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title") 132 } 133 } else { 134 System.out.println("搜到0条结果"); 135 } 136 } 137 138 }
以上是关于Elasticsearch5.0 Java Api -- 检索索引的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch5.0 Java Api -- 聚合查询
Elasticsearch5.0 Java Api -- 检索索引
Elasticsearch5.0 Java Api -- 更新索引
Elasticsearch5.0 Java Api -- 常用DSL查询