elasticsearch 创建索引,以及检索一条数据
2014-10-17 17:02
489 查看
elasticsearch的重要概念
我们可以把elasticsearch当做数据库来理解:index:索引库名称,相当于关系型数据库中的表名,一个elasticsearch集群中可以有多个索引库。
type:索引库中索引数据类型,为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。
id:索引库中索引数据主键,唯一。
创建json document
elasticsearch有多种创建json document的方式1. 手写,比如
String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}";
2. 使用map
Map<String, Object> json = new HashMap<String, Object>(); json.put("user","kimchy"); json.put("postDate",new Date()); json.put("message","trying out Elasticsearch");
3. 序列化bean
For example,use jackson<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.3</version> </dependency>
然后我们可以使用jackson来序列化我们的bean
import com.fasterxml.jackson.databind.*; // instance a json mapper ObjectMapper mapper = new ObjectMapper(); // create once, reuse // generate json String json = mapper.writeValueAsString(yourbeaninstance);
4.使用elasticsearch的帮助类
Elasticsearch 提供帮助类来生成JSON.import static org.elasticsearch.common.xcontent.XContentFactory.*; XContentBuilder builder = jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject()
创建索引
举个例子,比如索引名字叫blog,type是post,id为1IndexResponse response = client.prepareIndex("blog", "post", "1") .setSource(XContentFactory.jsonBuilder().startObject() .field("title", "test") .field("content", "here is content") .field("tag", "test") .endObject() ).execute().actionGet();
如果我们不指定id,ES会为我们生成id,setSource方法有几种形式,可以传入json字符串,map等,差不多就是上面指出的几种形式
IndexResponse返回一些信息:
// Index name String _index = response.getIndex(); // Type name String _type = response.getType(); // Document ID (generated or not) String _id = response.getId(); // Version (if it's the first time you index this document, you will get: 1) long _version = response.getVersion(); //是否创建成功 boolean isCreated = response.isCreated()
检索一条记录
在创建索引时,我们根据IndexResponse,得到了index、type和id,检索一条记录的方法很简单,它可以用来判断指定index,type,id的索引是否存在GetResponse getResponse = client.prepareGet("blog", "post","1") .execute() .actionGet();
GetResponse中常用的方法有isExists(),getSourceAsString()等,前者判断指定索引是否存在,后面用来得到返回的json。我们可以根据json反序列化得到我们要的对象
Post post = mapper.readValue(getResponse.getSourceAsString(),Post.class);
相关文章推荐
- Elasticsearch通过JAVA创建索引、Mapping以及数据的增删该查操作
- mysql 创建唯一索引前 删除重复数据只保留id最小一条。
- Elasticsearch(二)elasticsearch索引数据与简单检索GET一个文档
- elasticsearch-jdbc 使用数据库数据创建索引
- mssql2000数据库执行SQL语句来创建数据库以及数据表还有索引
- ElasticSearch速学 - 快速领悟:创建索引、api初步探索、查询数据
- ElasticSearch49:索引管理_快速上机动手实战创建、修改以及删除索引
- oracle 重建索引以及导出所有的索引脚本(可以解决还原数据库文件时先还原数据,在重新用脚本创建索引)
- 本文从如何建立mysql索引以及介绍mysql的索引类型,再讲mysql索引的利与弊,以及建立索引时需要注意的地方 首先:先假设有一张表,表的数据有10W条数据,其中有一条数据是nickname='
- elasticsearch核心知识--47.创建更新删除索引以及自定义分词器。
- elasticsearch核心知识--36._手动建立和修改mapping以及定制string类型数据是否分词或者是否被检索
- Elasticsearch生成json,创建索引(把数据写入索引中)
- SqlServer 创建聚集索引与非聚集索引处理千万条数据的优化,以及之间的区别
- mysql 创建唯一索引前 删除重复数据只保留ID最小一条。
- 索引的基本原理,以及数据是如何被访问的
- oracle 建表 造数据 存储过程 创建数据库的远程连接 以及一列的多行合并
- Oracle创建数据表空间,用户,授权,表,索引,簇
- 详解mysql索引总结----mysql索引类型以及创建
- 判断XML文件是否存在,不存在就创建一个XMl文件,存在就在里面添加一条数据
- 数据表的创建以及操作.三