目录
前言
所有操作基于kibana的 devs tools,相关的安装可以看:
【ES学习】ElasticSearch MacOS版 安装与使用(图文教程)
【ES学习】ElasticSearch在Kibana的使用 Kibana安装(MacOS版)
json数据形式
ES数据通过JSON形式存储
创建文档
PUT /索引index/类型type/文档id { JSON语句 }
我们输入:
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
继续加入一些文档:
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
检索文档 /index/type/id
GET /索引index/类型type/文档id
我们输入:
GET /megacorp/employee/1
得到:
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
_index:表示索引的名称
_type:表示类型名称
_id:表示文档id。
_version:表示这条文档的版本,因为我们没有修改过,所以版本是1,修改过之后会递增。
_seq_no:是序列号。
_primary_term:和_seq_no一样是一个整数,每当Primary Shard发生重新分配时,比如重启,Primary选举等,_primary_term会递增1。
found:查找成功为true,不成功为false。
_source:数据源,JSON本体
轻量搜索 /index/type/_search
我们输入:
GET /megacorp/employee/_search
得到:
{
"took" : 23,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
.....
..... (以下省略)
took:耗时
timed_out:是否超时
_shards.total:分片总数
hits.total:查询到的数量
hits.max_score:匹配度分数最大值
hits.hits:查询到的结果
hits._score:匹配度分数
查询概述
查询主要分为字段类查询和复合查询。
- 字段类查询:只针对某一个字段进行查询,如match、term等;
- 复合查询:可以进行一个或多个字段的查询,如bool查询等.
字段类查询主要包括两类:单词匹配和全文匹配。
- 单词匹配(Term Level Query):不对查询语句进行分词处理,直接匹配该字段的倒排索引;单词匹配包括term、terms、range、exist、ids、prefix、fuzzy、wildcard等查询语句:
- 全文匹配(Full Text Query):对指定的text类型的字段进行全文检索,会先对查询语句进行分词处理,例如你输入的查询文本是"我在马路边",es在分词器的作用下就会分词为"我"、“在”、"马路"这么几个单词,然后再去匹配。全文匹配包括match、match_phrase、multi_match、match_phrase_prefix、query_string、simple_query_string等查询语句;
query查询: match
match是最基本的基于全文检索的字段类查询语法
还记得我们的数据:
在字段about
中找出所有包含like
单词,我们输入:
GET /megacorp/employee/_search
{
"query" : {
"match": {
"about": "like"
}
}
}
得到:
{
"took" : 101,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.49376786,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "3",
"_score" : 0.49376786,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : [
"forestry"
]
}
},
......
......(以下省略)
我们可以得到两条数据,他们中的about
的字段都包含like
这个词。
如果我们输入:
GET /megacorp/employee/_search
{
"query" : {
"match": {
"about": "like climbing"
}
}
}
则会显示全部三个结果,说明单词之间是默认以"或"逻辑搜索的,而不需要真的出现"like climbing"这个词。
query查询: match - operator
query查询的match API还可以通过operator关键字,指定多个搜索词关系的匹配。(如果不指定operator就默认是“或”逻辑)
我们输入:
======== 多参数搜索表达式 ==========
GET /索引/类型/_search
{
"query" : {
"match": {
"搜索文档字段" : {
"query": "搜索单词",
"operator": "逻辑关系"
}
}
}
}
===================================
GET /megacorp/employee/_search
{
"query" : {
"match": {
"about" : {
"query": "go climbing",
"operator": "and"
}
}
}
}
这里我们的operator
是“与”逻辑关系,也就是说,必须是about
字段中同时出现go
和climbing
单词的文档doc,才能被搜索出来。
得到:
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.9155619,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.9155619,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}
query查询: match - minimum_should_match
minimum_should_match可以规定搜索词序列的最少包含个数。
比如此时搜索词序列为"to rock climbing",搜出来的文档必须至少包含to rock,或者rock climbing,只单独包含to的文档是搜不到的。
我们输入:
GET /megacorp/employee/_search
{
"query" : {
"match": {
"about" : {
"query": "to rock climbing",
"minimum_should_match": 2
}
}
}
}
得到:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
......
......
query查询: match_phrase
match_phrase也是对字段进行检索。
和match的区别在于:match是无序的,match_query是有顺序要求的。
我们输入:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase": {
"about" : "climbing rock"
}
}
}
结果什么都没搜出来。
query查询: match_phrase - slop
match_phrase可以添加参数slop,控制单词之间的允许间隔
GET /megacorp/employee/_search
{
"query" : {
"match_phrase": {
"about" : {
"query": "climbing rock",
"slop": 2
}
}
}
}
这次能搜出结果了。
query查询: query_string
query_string和 query查询的 match - operator 语句很像,也是在指定字段中搜索指定的(一串)搜索单词,并且可以用"与或非"逻辑关系加以限制。
搜索结果在about
字段中,不要有go 和 rock,我们输入:
=========== 表达式 =============
GET /megacorp/employee/_search
{
"query" : {
"query_string": {
"default_field": "目标字段",
"query": "搜索单词串 + AND/OR/NOT等逻辑"
}
}
}
==============================
GET /megacorp/employee/_search
{
"query" : {
"query_string": {
"default_field": "about",
"query": "NOT go AND rock"
}
}
}
得到:
{
"took" : 64,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.4589591,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.4589591,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}
也可以多字段进行搜索:
GET /megacorp/employee/_search
{
"query" : {
"query_string": {
"fields": ["about", "last_name"],
"query": "to AND Smith"
}
}
}
可以得到两个结果,他们既在about
字段包含 to单词,又在last_name
字段中包含Smith单词。
参考
官方中文文档
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_document_oriented.html