PUT shopping
GET shopping/_mapping
GET _cat/indices?v
DELETE employee
POST shopping/_doc { "title":"小米手机", "category":"小米", "images":"http://wwww.xiaomi.com", "price":119999 }
#创建文档添加数据 带id
POST shopping/_doc/1002 { "title":"小米手机", "category":"小米", "images":"http://wwww.xiaomi.com", "price":"1999" }
POST shopping/_create/1003 { "title":"小米手机", "category":"小米", "images":"http://wwww.xiaomi.com", "price":"19999" }
PUT shopping/_create/1005 { "title":"小米手机", "category":"华为", "images":"http://wwww.xiaomi.com", "price":"19999" }
PUT shopping/_doc/1001 { "title":"小米手机", "category":"小米", "images":"http://wwww.xiaomi.com", "price":"4999" }
POST shopping/_update/1002 { "doc":{ "title":"华为手机" } }
GET shopping/_doc/1002
GET shopping/_search
GET shopping/_search?q=category:小米 GET shopping/_search { "query": { "match": { "category": "小米" } } }
GET shopping/_search { "query": { "match_all": {} } }
GET shopping/_search { "query": { "match_all": { } }, "from": 0, "size": 2 }
GET shopping/_search { "query": { "match_all": { } }, "from": 0, "size": 2, "_source": "title" }
GET shopping/_search { "query": { "match_all": { } }, "from": 0, "size": 2, "_source": "title", "sort": [ { "category": { "order": "desc" } } ] }
#针对报错:Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are #disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [category] in order to load field data by uninverting the #inverted index. Note that this can use significant memory #经过查证是字段category类型为Text格式,然后涉及到了聚合排序等功能。没有进行优化,也类似没有加索引。没有优化的字段es默认是禁止聚合/排序操作的。所以需要将要聚合的字段添加优化。如下操作
PUT shopping/_mapping { "properties": { "category": { "type": "text", "fielddata": true } } } PUT shopping/_mapping { "properties": { "price": { "type": "text", "fielddata": true } } }
#1 GET shopping/_search { "query": { "bool": { "must": [ { "match": { "category": "小米" } } ] } } } #2 GET shopping/_search { "query": { "bool": { "must": [ { "match": { "category": "小米" } }, { "match": { "price": "1999" } } ] } } }
GET shopping/_search { "query": { "bool": { "should": [ { "match": { "category": "小米" } }, { "match": { "category": "华为" } } ] } } }
GET shopping/_search { "query": { "bool": { "should": [ { "match": { "category": "小米" } }, { "match": { "category": "华为" } } ], "filter": { "range": { "price": { "gt": 1999, "lt": 20000 } } } } } }
GET shopping/_search { "query": { "match": { "category": "小华" } } }
GET shopping/_search { "query": { "match_phrase": { "category": "小米" } }, "highlight": { "fields": { "category": {} } } }
GET shopping/_search { "aggs": { "price_group": { "terms": { "field": "price" } } } }
GET shopping/_search { "aggs": { "price_group": { "terms": { "field": "price" } } }, "size": 0 } ##
GET shopping/_search { "aggs": { "price_avg": { "avg": { "field": "price" } } }, "size": 0 }
#--------------------------------------------------------------------------- #报错 :Field [price] of type [text] is not supported for aggregation [avg] #解决办法如下 GET shopping/_mapping
PUT shopping1 PUT shopping1/_mapping {
"properties" : { "category" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "images" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "price" : { "type" : "long" }, "title" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } }
} POST _reindex { "source": { "index": "shopping" }, "dest": { "index": "shopping1" } } DELETE shopping POST _reindex { "source": { "index": "shopping1" }, "dest": { "index": "shopping" } } DELETE shopping1
#--------------------------------------------------------------
#先创建一个user 索引
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/_create/1001 { "name":"嚣张的小张", "sex":"男的", "tel":123123121231111 } #全局查询 (name类型为text 可以分词查询) GET user/_search { "query": { "match": { "name": "小" } } }
#全局查询 (sex类型为keyword 为关键字,不能够 被分开) GET user/_search { "query": { "match": { "sex": "男" } } } #全局查询 (tel 为关键字,不能被索引,所以不能被查询) GET user/_search { "query": { "match": { "tel": "123" } } }