1. 创建索引
- 要索引一个JSON对象,我们创建一个PUT请求到一个由索引名称,类型名称和ID组成的URL,即:http://localhost:9200/index/type/id,其中index和type是必须的,而id是可选的,如果不指定id,ES会为我们生成一个id,但此时应该使用post请求,而不是put;
- 索引名称是任意的,如果服务器上没有该索引,则将使用默认配置来创建一个索引;
- 我们可以把任何东西放到索引中,只要它可以表示为单个JSON对象,本教程我们以以下电影对象信息为例:
使用postman在正文中请求(PUT方式):
http://localhost:9200/movies/movie/1
body中的内容:
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}
返回信息:
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
2. 更新索引
- 使用相同的ID索引它,使用与之前完全相同的索引请求,但类型扩展了JSON对象;
- 请求方式(put):
请求地址:http://localhost:9200/movies/movie/1
body:
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}
返回:
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
可以观察到,响应结果与前面大体一致,但_version属性值更新为2,该属性值可用于跟踪文档以编入索引的次数。
3. 由ID获取文档/索引
- 请求方式:GET
请求url:http://localhost:9200/movies/movie/1 - 相应信息:
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_version": 2,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": [
"Crime",
"Drama"
]
}
}
"found"属性,表示文档已找到,并且操作成功;
"_source"属性,是一个非常重要的属性,它包含实际获取的文档信息。
4. 删除文档
- 请求方式:delete
请求url:http://localhost:9200/movies/movie/1 - 响应:
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
5. 搜索
- 首选,先添加一些示例数据:
id从1~6分别执行,会_bulk的话也可以使用_bulk单个请求执行批量插入
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}
{
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": ["Adventure", "Biography", "Drama"]
}
{
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": ["Crime", "Drama", "Mystery"]
}
{
"title": "Apocalypse Now",
"director": "Francis Ford Coppola",
"year": 1979,
"genres": ["Drama", "War"]
}
{
"title": "Kill Bill: Vol. 1",
"director": "Quentin Tarantino",
"year": 2003,
"genres": ["Action", "Crime", "Thriller"]
}
{
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": ["Biography", "Crime", "Drama"]
}
- _search,按照以下模式向URL发出请求:
localhost/9200/_search。其中,index和type都是可选的。
即,为了搜索电影,可以对以下任一url进行post请求:
(1)、http://localhost:9200/_search - 搜索所有索引和所有类型;
(2)、http://localhost:9200/movies/_search - 在电影索引中搜索所有类型;
(3)、http://localhost:9200/movies/movie/_search - 在电影索引中显式搜索电影类型的文档。 - 如果只是发送一个请求到上面的URL,我们会得到所有的电影信息。为了创建更有用的搜索请求,还需要向请求正文中提供查询。它主要包含一个名称为"query"的属性,这就可以使用ES的查询DSL。
{
"query": {
//Query DSL here
}
}
- 基于自由文本搜索
查询字符串查询是一个高级查询,有很多不同的选项,如果忽略了所有的可选参数,并且只需要给它一个字符串用于搜索,它就很容易使用。
eg:现尝试在两部电影的标题中搜索有"kill"这个词的电影信息:
请求方式:POST
请求URL:http://localhost:9200/_search
请求体:
{
"query": {
"query_string": {
"query": "kill"
}
}
}
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0467482,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "3",
"_score": 1.0467482,
"_source": {
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": [
"Crime",
"Drama",
"Mystery"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "5",
"_score": 1.0467482,
"_source": {
"title": "Kill Bill: Vol. 1",
"director": "Quentin Tarantino",
"year": 2003,
"genres": [
"Action",
"Crime",
"Thriller"
]
}
}
]
}
}
- 指定搜索的字段
上述搜索,没有指定要搜索的fileds,则将默认自动生成的名为"_all"的特殊字段,来基于所有文档中的各个字段匹配搜索。
我们可以使用"fields"属性,来指定要搜索的字段列表。
请求方式及请求URL:同上
请求体:
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}
响应:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.9794737,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "6",
"_score": 0.9794737,
"_source": {
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": [
"Biography",
"Crime",
"Drama"
]
}
}
]
}
}
6. 过滤
我们如果直接搜索"drama",不指定明确字段,我们将搜索到5条文档,现在如果想限制这些命中为只是1962年发布的电影,要做到这点,可以加相应的过滤器,让"year"字段等于1962
(注意,过滤查询已被弃用,并在ES 5.0中删除。现在应该使用bool / must / filter查询。)
{
"query": {
"bool": {
"must": {
"query_string": {
"query": "drama"
}
},
"filter": {
"terms": {
"year": [
1962
]
}
}
}
}
}