以下都是基于ES7.6.2版本的
接口风格REST风格
| method | url 地址 | 描述 |
|---|---|---|
| PUT | localhost:9200/索引名称/类型名称/文档 id | 创建文档(指定文档 id) |
| POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档 id) |
| POST | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档 |
| DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
| GET | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档 id |
| POST | localhost:9200/索引名称/类型名称/_seaarch | 查询所有数据 |
查看分词效果
GET /_analyze
{
"text": "今天吃了三个馍馍"
}
查看你创建的所有索引
GET /_cat/indices?v
查看文档类型 bank为索引
GET /bank/_mapping GET /bank
添加文档
PUT /bank/_doc/100000
{
"account_number":111,
"age":100
}
查看文档
GET /bank/_doc/100000
修改文档
POST /bank/_doc/100000/_update
{
"doc":{ "account_number" : 1122}
}
删除文档
DELETE /bank/_doc/100000
查询所有带排序和分页
GET /bank/_search
{
"query":{"match_all": {}},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 10,
"size": 5
}
带条件查询
对于数值类型match操作使用的是精确匹配,对于文本类型使用的是分词匹配
GET /bank/_search
{
"query":{"match": {
"age": "28"
}}
}
模糊查询
match 查询具有“fuziness” 属性。它可以被设置为 “0”, “1”, “2”或 “auto”。“auto” 是推荐的选项,它会根据查询词的长度定义距离。在实际的使用中,当我们使用 auto 时,如果字符串的长度大于5,那么 funziness 的值自动设置为2,如果字符串的长度小于2,那么 fuziness 的值自动设置为 0。fuziness的值代表可以容错的数。
以下例子当fuzziness为0时查询不到结果
https://elasticstack.blog.csdn.net/article/details/101287399
GET /bank/_search
{
"query": {
"match": {
"address": {
"query": "mil",
"fuzziness": 2
}
}
},
"highlight": {
"fields": {
"address": {}
}
},
"size": 1
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : 3.6021347,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 3.6021347,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
},
"highlight" : {
"address" : [
"990 Mill Road"
]
}
}
]
}
}
指定返回字段
GET /bank/_search
{
"query": {"match_all": {}},
"_source": ["age", "email"]
}
短语匹配
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "198 Mill"
}
}
}
term 不分词直接搜索 以下查询结果为空
term 在filter/query中使用,对搜索文本不分词,直接拿去倒排索引中匹配,你输入的是什么,就去匹配什么。
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "term": { "address": "198 Mill" } },
{ "term": { "address": "Bedell lane" } }
]
}
}
}
组合搜索
使用bool组合must表示同时满足
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
should表示满足其中任意一个
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
must_not表示同时不满足
filter 过滤搜索
filter 不计算相关性,同时可以cache,因此filter的速度快于query过滤查询。
gt 大于
gte 大于等于
lt 小于
lte 小于等于
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
聚合搜索
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
对sql的支持 http://www.macrozheng.com/#/reference/elasticsearch_sql_start
执行sql查询
POST /_sql
{
"query": "SELECt * FROM bank LIMIT 10"
}
SQL转化为DSL
POST /_sql/translate
{
"query": "SELECt * FROM bank LIMIT 10"
}
高亮查询
pre_tags、post_tags字段修改高亮表示,默认是、
GET /bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"highlight": {
"fields": {
"address": {}
}
},
"size": 1
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 5.4032025,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
},
"highlight" : {
"address" : [
"990 Mill Road"
]
}
}
]
}
}
Mapping详解几种查询的区别
termterm查询keyword
term不会分词,keyword字段存储时也不分词。需要完全匹配才行
term查询text字段
text存储时会分词,但是term不分词。想要term查询到的话:term查询的字段需要是text分词后的某个一个match
match查询keyword字段
match会被分词,keyword不会,需要match和keyword中完全匹配才行
match查询text字段
match分词,text也分词,分词结果匹配就行match_phrase[freɪz]
match_phrase匹配keyword
必须和keyword完全一致
match_phrase匹配text
match_phrase是分词的,text也是分词的。match_phrase的分词结果必须在text字段分词中都包含,而且顺序必须相同,而且必须都是连续的query_string
查询keyword
无法查询
查询text
和match_phrase区别的是,不需要连续,顺序还可以调换。
Mapping详解
nested 与 父子文档nested和父子文档



