elasticsearch 创建索引,以及检索一条数据
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为1
IndexResponse 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 创建索引,以及检索一条数据的更多相关文章
- Elasticsearch创建索引和映射结构详解
前言 这篇文章详细介绍了如何创建索引和某个类型的映射. 下文中[address]指代elasticsearch服务器访问地址(http://localhost:9200). 1 创建索引 ...
- elasticsearch创建索引
1.通过elasticsearch-head 创建 (1)登录localhost:9100 (2)点击复合查询 (3)输入内容 (4)勾选易读,点击验证是否是JSON格式 (5)点击提交请求,返回 { ...
- elasticsearch 创建索引
一.基本概念 索引:含有相同属性的文档的集合. //可以想象成一个数据库 database 类型:索引可以定义一个或多个类型,文档必须属于一个类型. //可以想象成数据库中的表 table 文档:文档 ...
- 前端处理:elementUI 表格索引代表第几条数据
分析:表格结合分页 知识点:1.表格的自定义索引(索引以当前行的行号作为参数)number, Function(index)该属性传入数字时,将作为索引的起始值.也可以传入一个方法,它提供当前行的行号 ...
- Elasticsearch 使用集群 - 创建索引
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- oracle 创建索引
一.索引简介 1.索引相当于目录 2.索引是通过一组排序后的索引键来取代默认的全表扫描检索方式,从而提高检索效率. 3.索引的创建要适度,多了会影响增删改的效率,少了会影响查询的效率,索引最好创建在取 ...
- PostgreSQL中COUNT的各条件下(1亿条数据)例子
test=# insert into tbl_time1 select generate_series(1,100000000),clock_timestamp(),now(); INSERT 0 1 ...
- [搜索]ElasticSearch Java Api(一) -添加数据创建索引
转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...
- ES 18 - (底层原理) Elasticsearch写入索引数据的过程 以及优化写入过程
目录 1 Lucene操作document的流程 1.1 添加document的流程 1.2 删除document的流程 2 优化写入流程 - 实现近实时搜索 2.1 流程的改进思路 2.2 设置re ...
随机推荐
- wpa_supplicant之eloop_run分析
部分内容转自http://blog.chinaunix.net/uid-20273473-id-3128151.html 重要结构体!!! struct eloop_sock { int sock; ...
- iOS开发热更新JSPatch
JSPatch,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug. 是否有过这样的经历 ...
- JSP在页面加载时调用servlet的方法
方法:先在JS里面写一个调用servlet的事件(可以利用ajax),然后利用<body>标签的onload调用这个事件. 代码如下: jsp文件代码如下: <%@ page lan ...
- 【Spring.Net】- 环境搭建
参考文章:http://www.cnblogs.com/GoodHelper/archive/2009/10/25/SpringNET_Config.html 一.环境下载及安装 到Spring的官方 ...
- chrome扩展程序中以编程方式插入内容脚本不生效的问题
chrome扩展程序中内容脚本有两种插入方式:(https://crxdoc-zh.appspot.com/extensions/content_scripts) 1. 清单文件: 这种方式会在打开每 ...
- Spring MVC 之@Controller@RequestMapping详解
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...
- Delphi Dataset CurValue
TField.CurValue Property Represents the current value of the field component including changes made ...
- 【Python】python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍“获得当前路径”“获得当前路径下的所有文件和文件夹, ...
- Spring Boot 运行原理
Spring Boot并没有任何新的技术,全都是基于Spring4提供的技术,用优秀的设计,为Web开发提供了一套新的方式. 在HelloWorld中,我们没有进行任何显示的配置,但是程序还是运行起来 ...
- filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行
filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行