创建索引,查询索引
#创建索引
PUT shopping
#查询全部索引
GET _cat/indices
测试数据源
PUT shopping/_doc/1
{
"name": "小米",
"title": "小米手机",
"price": 1999.0
}
PUT shopping/_doc/2
{
"name": "小米",
"title": "小米手机",
"price": 5991.0
}
PUT shopping/_doc/3
{
"name": "小米",
"title": "小米手机",
"price": 2342.0
}
PUT shopping/_doc/4
{
"name": "小米",
"title": "小米手机",
"price": 5432.0
}
PUT shopping/_doc/5
{
"name": "华为",
"title": "华为手机",
"price": 1999.0
}
PUT shopping/_doc/6
{
"name": "华为",
"title": "华为手机",
"price": 4352.0
}
PUT shopping/_doc/7
{
"name": "华为",
"title": "华为手机",
"price": 5991.0
}
删改
#删除
DELETE shopping/_doc/1
#修改ID为1的title数据
POST shopping/_update/1
{
"doc":{
"title":"小米电饭锅"
}
}
查询
#根据id查询
GET shopping/_doc/1
#查询全部数据
GET shopping/_search
#全量查询
GET shopping/_search
{
"query": {
"match_all": {
}
}
}
#分词条件查询
GET shopping/_search
{
"query": {
"match": {
"name": "小华"
}
}
}
#不分词条件查询
GET shopping/_search
{
"query": {
"match_phrase": {
"name": "小华"
}
}
}
分页查询(过滤字段,排序)
from size 分页查询
_source:对查询后的数据过滤只显示,title,name
sort是对price进行desc排序
GET shopping/_search
{
"query": {
"match_all": {
}
},
"from": 1,
"size": 2,
"_source": ["title","name"],
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
多条件查询(must=and,should=or)
#同时满足 小华并且price为5991
GET shopping/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "小华"
}
},
{
"match": {
"price": 5991
}
}
]
}
}
}
#满足小米或者华为,在过滤价格大于5000的数据
GET shopping/_search
{
"query": {
"bool": {
"should": [
{"match": {
"name": "小米"
}
},
{
"match": {
"name": "华为"
}
}
],
"filter": [
{"range": {
"price": {
"gte": 5000
}
}}
]
}
}
}
聚合查询
size:不要原数据
aggs:集合操作
price_group:名称,随意起名
terms:分组(操作函数 avg)
field:需要分组的字段
GET shopping/_search
{
"aggs": {
"price_group": {
"terms": {
"field": "price"
}
}
},
"size": 0
}
映射关系
index=false,该字段不可被查询
type字符串类型:
(1)text:通常用来存储长文本字段,例如博客标题、内容。通过定义analyzer,实现分词索引。
(2)keyword:不分词的字符串类型,存什么就是什么。
数值类型:long、integer、short、byte、double、float
日期类型:date
布尔类型:boolean
复合类型:
(1)object:包含一个子对象
(2)nested:包含一个或多个子对象
地理类型:geo_point
其他特定类型:ip、completion、join、alias等等
#创建索引
PUT user
#映射关系
PUT user/_mapping
{
"properties":{
"name": {
"type": "text",
"index":true
},
"sex": {
"type": "keyword",
"index":true
},
"tel": {
"type": "keyword",
"index":false
}
}
}
#查询索引映射
GET user/_mapping
#添加数据源
PUT user/_doc/1
{
"name": "小米",
"tel": 123113,
"sex": "男的"
}
GET user/_search
{
"query": {
"match": {
"name": "小米"
}
}
}
GET user/_search
{
"query": {
"match": {
"name": "男"
}
}
}
GET user/_search
{
"query": {
"match": {
"tel": "123113"
}
}
}