elasticsearch的集中常见操作
1.引入dependency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>1.4.0.M1</version>
</dependency>
2:创建\添加索引
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "4",
"_score": 1,
"_source": {
"name": "zhangyanan4",
"gender": "male",
"id": 4
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "5",
"_score": 1,
"_source": {
"name": "zhangyanan5",
"gender": "male",
"id": 5
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "6",
"_score": 1,
"_source": {
"name": "zhangyanan6",
"gender": "male",
"id": 6
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "1",
"_score": 1,
"_source": {
"name": "zhangyanan1",
"gender": "female",
"id": 1
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "2",
"_score": 1,
"_source": {
"name": "zhangyanan2",
"gender": "female",
"id": 2
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "7",
"_score": 1,
"_source": {
"name": "zhangyanan7",
"gender": "male",
"id": 7
}
}
,
{
"_index": "yananindex",
"_type": "yanantype",
"_id": "3",
"_score": 1,
"_source": {
"name": "zhangyanan3",
"gender": "female",
"id": 3
}
}
3.创建实体类
package testweb; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName="yananindex",type="yanantype")
public class Yanan {
private int id;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
准备工作做完了,下面开始测试
4.queryForObject\queryForList\聚合查询\统计数量\判断索引是否存在\删除指定id的索引
package testweb; import java.util.List;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; public class TestES {
static TransportClient transportClient;
static ElasticsearchTemplate elasticsearchTemplate; public static void main(String[] args) {
elasticsearchTemplate=getElasticsearchTemplate("localhost","elasticsearch_bak",9303);
//queryForObject();//根据索引id查询文档
//queryForList();
//aggregationQuery();//聚合查询
//count();
//indexExists(Yanan.class);
deleteById();
}
private static void deleteById() {
// IndexQueryBuilder indexquery=new IndexQueryBuilder().withId("1").withObject(Yanan.class);
//String delete = elasticsearchTemplate.delete("yananindex", "yanantype", "10");//索引的id
String delete = elasticsearchTemplate.delete(Yanan.class, "10");//索引的id
System.out.println(delete);//删除的索引的id
}
private static void indexExists(Class<Yanan> clazz) {
if(elasticsearchTemplate.indexExists(clazz)){
System.out.println("true");
}else{
System.out.println(false);
}
}
private static void count() {
NativeSearchQueryBuilder searchQuery=new NativeSearchQueryBuilder();
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
bqb.must(QueryBuilders.termQuery("gender","female"));
searchQuery.withIndices("yananindex").withQuery(bqb);
long count = elasticsearchTemplate.count(searchQuery.build());
System.out.println(count);
} private static void aggregationQuery() {
TermsBuilder termsBuilder = AggregationBuilders.terms("maleCount").field("name").size(10);
NativeSearchQueryBuilder searchQuery=new NativeSearchQueryBuilder();
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
bqb.must(QueryBuilders.termQuery("gender","male"));
searchQuery.withIndices("yananindex").withQuery(bqb).addAggregation(termsBuilder);
Aggregations aggregations = elasticsearchTemplate.query(searchQuery.build(), new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
StringTerms modelTerms = (StringTerms)aggregations.asMap().get("maleCount");
List<Bucket> buckets = modelTerms.getBuckets();
for(int i=0;i<buckets.size();i++){
System.out.println(buckets.get(i).getKey()+"\t"+buckets.get(i).getDocCount());
}
}
private static void queryForList() {
NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
bqb.must(QueryBuilders.termQuery("gender","male"));
searchQuery.withIndices("yananindex").withQuery(bqb);
List<Yanan> list = elasticsearchTemplate.queryForList(searchQuery.build(), Yanan.class);
for(int i=0;i<list.size();i++){
Yanan yanan = list.get(i);
System.out.println(yanan.getName()+"\t"+yanan.getGender()+"\t"+yanan.getId());
} }
private static void queryForObject() {
GetQuery query = new GetQuery();
query.setId("1");// 索引的id
Yanan yanan = elasticsearchTemplate.queryForObject(query, Yanan.class);
System.out.println(yanan.getGender());
System.out.println(yanan.getName());
System.out.println(yanan.getId());// 实体的id
} static ElasticsearchTemplate getElasticsearchTemplate(String ip,String clusterName,int port){//<a href="http://blog.csdn.net/ty4315/article/details/52434296" target="_blank">@参考文章1</a>,<a href="http://blog.csdn.net/ljc2008110/article/details/48630609" target="_blank">@参考文章2</a>
Settings setting = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName)// 指定集群名称
.put("client.transport.sniff", true)// 启动嗅探功能,客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中,这样做的好处是一般你不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器。
.build();
// 创建客户端 通过setting来创建,若不指定则默认链接的集群名为elasticsearch 链接使用tcp协议即9303
transportClient = new TransportClient(setting);
TransportAddress transportAddress = new InetSocketTransportAddress(ip, port);
transportClient.addTransportAddresses(transportAddress);
elasticsearchTemplate = new ElasticsearchTemplate(transportClient);
return elasticsearchTemplate;
}
}
elasticsearch的集中常见操作的更多相关文章
- 动态单链表的传统存储方式和10种常见操作-C语言实现
顺序线性表的优点:方便存取(随机的),特点是物理位置和逻辑为主都是连续的(相邻).但是也有不足,比如:前面的插入和删除算法,需要移动大量元素,浪费时间,那么链式线性表 (简称链表) 就能解决这个问题. ...
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)
CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作
http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...
- C#路径/文件/目录/I/O常见操作汇总
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- X-Cart 学习笔记(四)常见操作
目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...
- 转:jQuery 常见操作实现方式
http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...
- jQuery 常见操作实现方式
一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备忘. jQuery 常见操作实现方式 $("标签名") //取html元素 document. ...
- C#路径/文件/目录/I/O常见操作汇总<转载>
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组
1.数组基础 1.什么是数组: 同一类型数据的集合,就是一个容器. 2.数组的好处: 可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式: (一 ...
随机推荐
- Box布局
import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidget): def __init__(self, paren ...
- ActiveMQ进阶学习
本文主要讲述ActiveMQ与spring整合的方案.介绍知识点包括spring,jms,activemq基于配置文件模式管理消息,消息监听器类型,消息转换类介绍,spring对JMS事物管理. 1. ...
- 如何在一个项目中同时包含mvc建站、webapi接口
项目做得多了..就会发现有些小项目不想建太多的项目..现在思明在这里和大家分享一下如果再一个项目中同时包含mvc建站以及实现webapi接口 1.新建项目 aps.net web 应用程序 2 新建模 ...
- Zabbix实战-简易教程--拓扑图(Maps)
一.拓扑图(Maps) 二话不说,有图有真相,先看看效果,再详细讲解配置过程: 图1:全国网络质量图 图2 核心机房网络质量图 二.详细配置 1.添加 map 选择 系统管理-->基础配置-- ...
- 利用cmd制作一句话图片马
先cd进在图片和一句话木马里面的文件夹 假设选择我的图片是:1.jpg 一句话是:2.php 命令:copy 1.jpg/a+2.php/b 生成的图片马
- Codeforces 626B Cards(模拟+规律)
B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...
- [Codeforces 696D] Legen...
题目大意: 给出一些匹配串,要造一个长度不超过L的字符串,每个匹配串有自己的价值,匹配串每次出现在字符串里都会贡献一次价值...要求可能得到的最大价值. 匹配串总长不超200,L<=10^14, ...
- hdu_4869(费马小定理+快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 Turn the pokers Time Limit: 2000/1000 MS (Java/O ...
- ThoughtWorks University之旅 —— 印度游记
ThoughtWorks University是ThoughtWorks为新加入的员工提供的入职培训项目之一,会将世界各地office新入职的员工一起带到印度浦那,参加一次为期5周的培训,内容涵盖了公 ...
- HDU 1979 Red and Black
题目: There is a rectangular room, covered with square tiles. Each tile is colored either red or black ...