首页>SQL技巧教程>文章详情

elasticsear 安装 java集成使用

3/7/2017
1322

Elasticsearch查询操作

Elasticsearch部署

通过SSH Secure Shell连接工具,将ES文件上传到linux系统相应目录,解压 在linux环境,进入ES目录的bin目录运行命令./elasticsearch安装es-head插件,进入elasticsearch/bin目录,输入命令./plugin  install mobz/elasticsearch-head 安装head插件。修改elasticsearch.yml:新增cluster.name: pangu;node.name: node-1;network.host: 192.168.45.31在浏览器中输入http://localhost:9200,http://localhost:9200/_plugin/head/如下图所示则ES启动成功。

Elasticsearch文档格式

索引中最基本的单元叫做文档 document. 在es中文档的示例如下:

{

"_index": "questions",

"_type": "baichebao",

"_id": "4",

"_score": 1,

"_version" : 1,

"_source": {

"id": 4,

"content": "汽车常见故障的解决办法有哪些?",

"uid": 1,

"all_answer_count": 2,

"series_id": 0,

"score": 0,

"answer_count": 2

}

文档中下划线开头的是es自带的字段

_index 代表索引名_type 代表类型_id 代表文档id,如果插入文档的时候没有设置id的话,那么es会自动生成一个唯一id_score 这个不是文档自带的,而是进行搜索的时候返回的,代表这个文档和搜索的相关匹配分值_source 储存原始文本及分类好的字段_version 代表这个文档的版本

Restful结构化查询(DSL)

一:terms查询

         如果你想要找到所有售价等于10000美刀的车,那么可以使用一个terms查询:

GET /cars/transactions/_search

{

  "query": {

    "term": {

      " price ": "10000"

    }

  }

}

二:filtered查询

如果你想要找到所有售价高于10000美刀的车,那么可以使用一个filtered查询:

POST /cars/transactions/_search

{

    "query" : {

        "filtered": {

            "filter": {

                "range": {

                    "price": {

                        "gte": 10000

                    }

                }

            }

        }

    }

}

 

该查询(包含了一个过滤器)返回文档的一个特定子集,然后聚合工作在该子集上。

 

三:agg聚合查询

对索引中全部的车计算其平均价格

POST /cars/transactions/_search

{

  "aggs": {

    "car_avg": {

      "avg": {

         "field": "price"

      }

    }

  }

}

 

四:过滤聚合联合查询

如果你想要找到所有售价高于10000美刀的车,同时也对这些车计算其平均价格,那么可以这样查询:

POST /cars/transactions/_search

{

    "query" : {

        "filtered": {

            "filter": {

                "range": {

                    "price": {

                        "gte": 10000

                    }

                }

            }

        }

    },

    "aggs" : {

        " car_avg ": {

            "avg" : { "field" : "price" }

        }

    }

五:聚合嵌套查询

如果你想要找到不同颜色的车的数量,并且计算不同颜色的车的平均价钱,那么可以这样查询

POST /cars/transactions/_search

{

  "aggs": {

    "colors": {

      "terms": {

        "field": "color"

      },

      "aggs": {

        "avg_price": {

          "avg": {

            "field": "price"

          }

        }

      }

    }

  }

}

 

六:聚合并列查询

如果你想要找到不同颜色的车的数量,还要知道全部车的平均价钱,那么可以这样查询

{

  "aggs": {

    "avg_price": {

      "avg": {

        "field": "price"

      }

    },

    "colors": {

      "terms": {

        "field": "color"

      }

    }

  }

}

七:综合查询

         场景:查询出某一天adtype(广告位id)为2的数据每小时有几条,每小时的总收益和平均收益

{

  "query": {

    "bool": {

      "must": [

        {

          "term": {

            "adtype": "2"

          }

        },

        {

          "match_all": {}

        }

      ],

      "must_not": [],

      "should": []

    }

  },

  "from": 0,

  "size": 20,

  "sort": [],

  "aggs": {

    "adxtime": {

      "date_histogram": {

        "field": "adx_reporttime",

        "interval": "1h",

        "min_doc_count": 0

      },

      "aggs": {

        "sum_price": {

          "sum": {

            "field": "price"

          }

        },

        "avg_price": {

          "avg": {

            "field": "price"

          }

        }

      }

    }

  }

}

Java client查询

一:获取es客户端

         Settings settings = Settings.settingsBuilder()

                                  .put("cluster.name", "pangu")

                                  .put("client.transport.sniff", true) //允许嗅探

                                  .build();

         Client client = TransportClient.builder()

                                  .settings(settings) //设置部分

                                  .build()

                                  .addTransportAddress(

new InetSocketTransportAddress(InetAddress.getByName("192.168.33.203"), 9300));

                                  .addTransportAddress(

new InetSocketTransportAddress(InetAddress.getByName("192.168.33.204"), 9300));

 

二:简单查询(对应restful查询第一点)

         BoolQueryBuilder qb = QueryBuilders.boolQuery();

         qb.must(new  QueryStringQueryBuilder("10000").field("" price "));

         SearchResponse response = client

                                     .prepareSearch("cars ")

                                     .setTypes("transactions ")

                                     .setSearchType(SearchType.DEFAULT).setFrom(0).setSize(10)

                                     .setQuery(qb)

                                     .execute().actionGet();

for (SearchHit hit : response.getHits().getHits()) {

         System.out.println(hit.getSource().get("price"));

}

三:聚合嵌套查询(对应restful查询第五点)

             AggregationBuilder aggs = AggregationBuilders.terms("colors").field(

                                     "color");

                   aggs.subAggregation(AggregationBuilders.avg("avg_price").field("price"));

                  

                   SearchResponse response = client

                                     .prepareSearch("cars")

                                     .setTypes("transactions")

                                     .setSearchType(SearchType.DEFAULT)

                                     .addAggregation(aggs)

                                     .execute().actionGet();

                  

                   Terms terms = response.getAggregations().get("colors");

                   for (Bucket b : terms.getBuckets()) {

                            System.out.print(b.getKey() + "有:");

                            System.out.println(b.getDocCount() + " 辆");

                           

                            Avg avg =  b.getAggregations().get("avg_price");

                            System.out.print(b.getKey() + "平均:");

                            System.out.println(avg.getValue()+ "元");

                   }

        

 

四:聚合并列查询(对应restful查询第六点)

AggregationBuilder aggs1 = AggregationBuilders.terms("avg_price").field(

                                     "price");

                   AggregationBuilder aggs2 = AggregationBuilders.terms("colors").field(

                                     "color");

                  

                   SearchResponse response = client

                                     .prepareSearch("cars")

                                     .setTypes("transactions")

                                     .setSearchType(SearchType.DEFAULT)

                                     .addAggregation(aggs1)

                                     .addAggregation(aggs2)

                                     .execute().actionGet();

                   Avg avg =  response.getAggregations().get("avg_price");

                   System.out.println(avg.getValue()+ "元");

 

                  

                   Terms terms = response.getAggregations().get("colors");

                   for (Bucket b : terms.getBuckets()) {

                            System.out.print(b.getKey() + "有:");

                            System.out.println(b.getDocCount() + " 辆");

                   }

 

五:综合查询

    场景:查询出某一天adtype(广告位id)为2的数据每小时有几条,每小时的总收益和平均收益

String str = "ad_detail_model";

String indexs[] = str.split(",");

BoolQueryBuilder qb = QueryBuilders.boolQuery();

qb.must(new  QueryStringQueryBuilder("2").field("adtype"));

DateHistogramInterval d1 = new DateHistogramInterval("1h");

AggregationBuilder aggs1 = AggregationBuilders.dateHistogram("timeAgg")

.field("adx_reporttime").interval(d1).minDocCount(0);

aggs1.subAggregation(AggregationBuilders.sum("sumAgg").field("price"));

aggs1.subAggregation(AggregationBuilders.avg("avgAgg").field("price"));

SearchResponse response = client

                                     .prepareSearch(indexs)

                                     .setSearchType(SearchType.DEFAULT)

                                     .setQuery(qb)

                                     .addAggregation(aggs1)

                                     .execute().actionGet();

Histogram agg = response.getAggregations().get("timeAgg");

System.out.println("===================================");

for (Histogram.Bucket entry : agg.getBuckets()) {

                            System.out.println("*******************************************");

                            Sum sum =  entry.getAggregations().get("sumAgg");

                            System.out.print(entry.getKey() + "总共有:");

                            System.out.println(sum.getValue()+ "元");

                           

                            Avg avg =  entry.getAggregations().get("avgAgg");

                            System.out.print(entry.getKey() + "平均有:");

                            System.out.println(avg.getValue()+ "元");

                           

                            String key = ""+ entry.getKey();    // Key

                       String keyAsString = entry.getKeyAsString(); // Key as String

                       long docCount = entry.getDocCount();         // Doc count

                       System.out.println(keyAsString+"有:"+docCount+" 个");

                       System.out.println("*******************************************");

                   }

System.out.println("===================================");