查看节点信息
GET /_cat/nodes?v
查看所有索引信息
GET /_cat/indices?v
创建索引
PUT /customer?pretty
查看指定索引信息
GET /customer?pretty
添加数据
PUT /customer/_doc/1?pretty
{
"name": "张三"
}
根据id查询
GET /customer/_doc/1?pretty
PUT /customer/_doc/2?pretty
{
"name": "李四"
}
PUT /customer/_doc/3?pretty
{
"name": "王五"
}
PUT /customer/_doc/4?pretty
{
"name": "赵六"
}
PUT /customer/_doc/5?pretty
{
"name": "张无忌"
}
PUT /customer/_doc/6?pretty
{
"name": "张无天"
}
删除索引
DELETE /customer?pretty
查询所有
GET /customer/_doc/_search?pretty
同上
GET /customer/_doc/_search
{
"query": {
"match_all":{}
}
}
match查询(根据条件查询)
GET /customer/_doc/_search
{
"query": {
"match": {
"name": "张"
}
}
}
排序
GET /customer/_doc/_search
{
"query": {
"match_all":{}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
分页
GET /customer/_doc/_search
{
"query": {
"match_all":{}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 4,
"size": 2
}
match查询(短语查询)
GET /customer/_doc/_search
{
"query": {
"match_phrase": {
"name": "张无"
}
}
}
match查询(最左前缀查询)
GET /customer/_doc/_search
{
"query": {
"match_phrase_prefix": {
"name": "张无忌"
}
}
}
分词
POST /_analyze
{
"analyzer": "standard",
"text": "中国人"
}
使用PUT用于新增和修改信息,修改时必须传入所有字段。否则丢失部分数据。
只是修改部分字段,可以使用POST。
修改制定字段使用POST
POST index/type/id/_update
{
"doc": {
"field": "value"
}
}
使用POST不指定id,会新增一条文档,id自动生成。
POST /customer/_doc?pretty
{
"name": "我是POST进来的",
"age": 28
}
布尔查询
布尔查询
must(and)
should(or)
must_not(not)
filter
### name包含张 并且 age等于28
GET /customer/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张"
}
},
{
"match": {
"age": "28"
}
}
]
}
}
}
### name包含张 或者 age等于28
GET /customer/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "张"
}
},
{
"match": {
"age": "28"
}
}
]
}
}
}
### name包含张 并且 age大于18
GET /customer/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张"
}
}
],
"filter": {
"range": {
"age": {
"gt": 18
}
}
}
}
}
}
filter
工作于bool
查询内
### age大于18
GET /customer/_doc/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gt": 18
}
}
}
}
}
}
range 条件筛选范围
gt 大于
gte 大于等于
lt 小于
lte 小于等于
高亮查询
### name包含张 并且 age大于18
GET /customer/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张"
}
}
],
"filter": {
"range": {
"age": {
"gt": 18
}
}
}
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
将需要高亮的字段添加到fields内
自定义高亮显示
### name包含张 并且 age大于18
GET /customer/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张无"
}
}
],
"filter": {
"range": {
"age": {
"gt": 18
}
}
}
}
},
"highlight": {
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"name": {}
}
}
}
#### 结果字段过滤 _source
GET /customer/_doc/_search
{
"query": {
"match": {
"name": "张"
}
},
"_source": ["name", "age"]
}
聚合查询 aggs
sum 求和
avg 平均值
max 最大值
min 最小值
group 分组
也可以组内聚合
#### 查询所有姓名包含张,年龄总和
GET /customer/_doc/_search
{
"query": {
"match": {
"name": "张"
}
},
"_source": ["name", "age"],
"aggs": {
"sumAge": {
"sum": {
"field": "age"
}
}
}
}
映射类型
https://www.cnblogs.com/Neeo/articles/10393961.html
#### 创建索引并指定映射关系
PUT /order
{
"mappings": {
"_doc": {
"dynamic": false,
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "double"
}
}
}
}
}
#### 查看映射
GET /order/_mapping
返回
{
"order" : {
"mappings" : {
"_doc" : {
"dynamic" : "false",
"properties" : {
"name" : {
"type" : "text"
},
"price" : {
"type" : "double"
}
}
}
}
}
}
dynamic 为false时,新增数据时添加的新字段没有映射关系,无法做为查询条件。dynamic值默认为true。
dynamic 为strict时,新增数据时无法添加新的字段。
mappings 属性设置 https://www.cnblogs.com/Neeo/articles/10395524.html
https://www.cnblogs.com/Neeo/articles/10789701.html
mappings设置ignore_above
长度超过ignore_above的字符串将不会被索引。
设置属性索引index
PUT /product
{
"mappings": {
"doc": {
"dynamic": false,
"properties": {
"name": {
"type": "text",
"index": true
},
"age": {
"type": "long",
"index": false
}
}
}
}
}
index 属性默认值为true,如果设置为false,那么ElasticSearch不会为该属性创建索引,也就是说无法做为主查询条件。
#### 对象属性,info 既是一个属性,也是一个对象。
PUT /user/_doc/1
{
"name": "张三",
"age": 18,
"info": {
"addr": "北京",
"tel": "10086"
}
}
PUT /user/_doc/2
{
"name": "李四",
"age": 22,
"info": {
"addr": "上海",
"tel": "10088"
}
}
GET /user/_mapping
#### 根据info 的addr 查询
GET /user/_doc/_search
{
"query": {
"match": {
"info.addr": "上海"
}
}
}