首页 技术 正文
技术 2022年11月6日
0 收藏 955 点赞 652 浏览 2685 个字

Sense

为了方便、直观的使用es的REST Api,我们可以使用sense。Sense是Chrome浏览器的一个插件,使用简单。

如图:

elasticsearch 口水篇(2)CRUD Sense

Sense安装:

https://chrome.google.com/webstore/detail/sense/doinijnbnggojdlcjifpdckfokbbfpbo

或者直接去chrome网上应用店搜索安装。

elasticsearch 口水篇(2)CRUD Sense


CRUD

URL的格式:

http://localhost:9200/<index>/<type>/[<id>]

其中index、type是必须提供的。

id是可选的,不提供es会自动生成。

index、type将信息进行分层,利于管理。index可以理解为数据库,type理解为数据表。

补一张图(0211)

elasticsearch 口水篇(2)CRUD Sense

The type called another_type and the index called another is shown in order to emphasize that Elasticsearch is multi-tenant, by which we mean that a single server can store multiple indexes and multiple types.

上面解释了“多租户”!

索引文档(Create、update)

首先我们塞入一条数据,命令如下:

curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}'

-d代表一个json格式的对象,这里是一部电影数据。

运行结果如下:

elasticsearch 口水篇(2)CRUD Sense

通过默认查询我们可以查询到刚才添加的数据,如下:

elasticsearch 口水篇(2)CRUD Sense

下面我们来修改这条数据,添加电影的类型,如下:

curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'

再查询可以发现该数据多了一个字段:genres。

elasticsearch 口水篇(2)CRUD Sense

通过id进行查询(getting by id)

curl -XGET "http://localhost:9200/movies/movie/1" -d''

elasticsearch 口水篇(2)CRUD Sense

删除文档(deleting document)

curl -XDELETE "http://localhost:9200/movies/movie/1" -d''

elasticsearch 口水篇(2)CRUD Sense

再通过getting by id,返回:

elasticsearch 口水篇(2)CRUD Sense

检索(search)

为了检索我们先多添加几篇文档:

curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'curl -XPUT "http://localhost:9200/movies/movie/2" -d'
{
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": ["Adventure", "Biography", "Drama"]
}'curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": ["Crime", "Drama", "Mystery"]
}'curl -XPUT "http://localhost:9200/movies/movie/4" -d'
{
"title": "Apocalypse Now",
"director": "Francis Ford Coppola",
"year": 1979,
"genres": ["Drama", "War"]
}'curl -XPUT "http://localhost:9200/movies/movie/5" -d'
{
"title": "Kill Bill: Vol. 1",
"director": "Quentin Tarantino",
"year": 2003,
"genres": ["Action", "Crime", "Thriller"]
}'curl -XPUT "http://localhost:9200/movies/movie/6" -d'
{
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": ["Biography", "Crime", "Drama"]
}'

  

最好再参考下:ElasticSearch’s query DSL

{
"query": {
//Query DSL here
}
}

  

–基本的文本检索

curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "kill"
}
}
}'

  

–指定字段进行检索

curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}'

fields默认为”_all” 。

–过滤(filtering)

curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}'

  

详细api,参考:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html

部分内容摘抄自:

http://joelabrahamsson.com/elasticsearch-101/


query VS filter

摘录一张有意思的ppt。

elasticsearch 口水篇(2)CRUD Sense

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289