Elasticsearch(es)自定义mapping创建索引(二)_海涛高软-CSDN博客
列表查询所有索引,并且以列表展示 :
GET _cat/indices?v
准备好下面是数据:
总共id,name,age,address,other五个字段
要求 :
name 不用检索
other 全文检索
address 后续做聚合分桶
其它字段全部为keyword类型
写一下mapping
索引名为:my_test
Type名为:children
PUT /my_test { "mappings": { "children": { "properties": { "id": { "type": "integer" }, "name": { "type": "text", "index": false }, "age": { "type": "integer" }, "address": { "type": "keyword" }, "other": { "type": "text", "analyzer": "ik_max_word" } } } } }
查看一下刚刚建立的索引的mapping
GET my_test/_mapping
查询结果如下:
备注 删除索引:
DELETe my_test
按照下面的方式将文章开始时准备的数据添加进索引
PUT my_test/children/1 { "id": 1, "name": "海涛", "age": 37, "address": "信阳市", "other": "重点倾向于自定义内容" } PUT my_test/children/2 { "id": 2, "name": "刘能", "age": 25, "address": "南阳市", "other": "本篇接着前一篇内容" } PUT my_test/children/3 { "id": 3, "name": "赵四", "age": 37, "address": "南阳市", "other": "数组集合类的底层结构自定义" } PUT my_test/children/4 { "id": 4, "name": "王五", "age": 29, "address": "郑州市", "other": "还是以music索引为例数组" } PUT my_test/children/5 { "id": 5, "name": "杨娟", "age": 37, "address": "郑州市", "other": "可以为索引增加新的" } PUT my_test/children/6 { "id": 6, "name": "王平", "age": 12, "address": "驻马店市", "other": "索引时要注意的是数组内的元素" } PUT my_test/children/7 { "id": 7, "name": "国柱", "age": 85, "address": "周口市", "other": "重点索引集合类的底层结构前一篇" }
使用下面命令查询my_test索引/children类型下的所有文档
GET my_test/children/_search
根据id查询某个具体的文档使用 :
GET my_test/children/5
删除指定id的文档:
DELETE my_test/children/5
查看某个索引的设置信息:
GET my_test/_settings
结果:
{ "my_test": { "settings": { "index": { "creation_date": "1635325226039", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "3Qz7p1B7ScCz6V6jAwVzwA", "version": { "created": "6030199" }, "provided_name": "my_test" } } } }
下面两种,不管是match还是term查询, address为南阳则查询结果0条,address为南阳市则查询2条
原因是: address字段 是 keyword 类型
GET my_test/children/_search { "query":{ "term":{ "address": "南阳" } } }
GET my_test/children/_search { "query":{ "match": { "address": "南阳" } } }
报错, "reason": "Cannot search on field [name] since it is not indexed."
原因: name字段 被设置 index=false 表示不建立索引
GET my_test/children/_search { "query":{ "match": { "name": "海涛" } } }
命中3条,下面这是查询age大于等于11 ,且小于等于36的所有文档
GET my_test/children/_search { "query":{ "range": { "age": { "gte": 11, "lte": 36 } } } }
id字段值的总和,age字段聚合分桶
GET my_test/children/_search { "aggs": { "avg_age": { "terms": { "field": "age" } }, "sum_id": { "sum": { "field": "id" } } } }
结果:
"aggregations": { "avg_age": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 37, "doc_count": 3 }, { "key": 12, "doc_count": 1 }, { "key": 25, "doc_count": 1 }, { "key": 29, "doc_count": 1 } ] }, "sum_id": { "value": 21 } }
查询年龄大于等于29
GET my_test/children/_search { "aggs":{ "count_age": { "filter": { "range": { "age": { "from": 29 } } } } } }
结果:
"aggregations": { "count_age": { "doc_count": 4 } }
GET my_test/children/_search { "size": 2, "aggs": { "quchong_ages": { "cardinality": { "field": "age" } } } }
结果:
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 6, "max_score": 1, "hits": [ { "_index": "my_test", "_type": "children", "_id": "4", "_score": 1, "_source": { "id": 4, "name": "王五", "age": 29, "address": "郑州市", "other": "还是以music索引为例数组" } }, { "_index": "my_test", "_type": "children", "_id": "2", "_score": 1, "_source": { "id": 2, "name": "刘能", "age": 25, "address": "南阳市", "other": "本篇接着前一篇内容" } } ] }, "aggregations": { "quchong_ages": { "value": 4 } } }
GET my_test/children/_search { "aggs": { "fentong_ages": { "terms": { "field": "age", "size": 2 }, "aggs": { "ratedddd": { "top_hits": { "sort": [{ "id": {"order": "desc"} }], "size": 2 } } } } } }
意思:
按照降序查询所有,并且取出前3条
GET my_test/children/_search { "query":{ "match_all":{ } }, "size":3, "from":0, "sort": [ { "id": { "order": "desc" } } ] }
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)