【ElasticSearch 6.*】 学习八:查询数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31617637/article/details/85105581

数据准备

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "noval":{
            "properties":{
            	"word_count":{
            		"type": "integer"
            	},
                "author":{
                    "type":"keyword"
                },
                "title":{
                    "type":"text"
                },
                "publish_date":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH-mm-ss||yyyy-MM-dd"
                }       
            }
        }
    }
}
  • 插入数据
    在这里插入图片描述

简单查询

根据id查询
请求连接(GEThttp://localhost:9200/book/noval/1
返回值

{
    "_index": "book",
    "_type": "noval",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "title": "ekasticsearch v1.0",
        "author": "luxiaofeng",
        "word_count": "28",
        "publish_date": "1990-09-20"
    }
}

按条件查询

{
	"query": {
		"match_all":{}
	}
}

返回值:
took:
total:查询到的总条数
hits:查询到的条数列表(默认返回10条

{
    "took": 1252,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v2.0",
                    "author": "huamanlou",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "李寻欢啊",
                    "author": "古龙",
                    "word_count": "1221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "title": "花满楼啊",
                    "author": "古龙",
                    "word_count": "22",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "title": "丁鹏啊",
                    "author": "金庸",
                    "word_count": "2221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "title": "郭靖啊",
                    "author": "金庸",
                    "word_count": "212",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "12",
                "_score": 1,
                "_source": {
                    "title": "鸠摩智啊",
                    "author": "金庸",
                    "word_count": "44",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v1.0",
                    "author": "luxiaofeng",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "楚留香啊",
                    "author": "古龙",
                    "word_count": "2811",
                    "publish_date": "1990-09-20"
                }
            }
        ]
    }
}
  • 根据传递值来返回条数
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数(条件):
    from: 第几条开始返回
    size: 返回几条信息
{
	"query": {
		"match_all":{}
	},
	"from": 1,
	"size": 1
}

返回值:

{
    "took": 852,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                }
            }
        ]
    }
}
  • 关键词的查询
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数(条件):
    match:关键字查询的
    sort:根据谁排序
    order: Desc 从大到小 ase 从小到大
{
	"query": {
		"match":{
			"title":"啊"
		}
	},
	"sort":{
		"publish_date":{
			"order": "desc"
		}
	}
}

返回值:

{
    "took": 5059,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": null,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "3",
                "_score": null,
                "_source": {
                    "title": "楚留香啊",
                    "author": "古龙",
                    "word_count": "2811",
                    "publish_date": "1990-09-20"
                },
                "sort": [
                    653788800000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": null,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "5",
                "_score": null,
                "_source": {
                    "title": "李寻欢啊",
                    "author": "古龙",
                    "word_count": "1221",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "6",
                "_score": null,
                "_source": {
                    "title": "花满楼啊",
                    "author": "古龙",
                    "word_count": "22",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": null,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "11",
                "_score": null,
                "_source": {
                    "title": "丁鹏啊",
                    "author": "金庸",
                    "word_count": "2221",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "8",
                "_score": null,
                "_source": {
                    "title": "郭靖啊",
                    "author": "金庸",
                    "word_count": "212",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "12",
                "_score": null,
                "_source": {
                    "title": "鸠摩智啊",
                    "author": "金庸",
                    "word_count": "44",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "9",
                "_score": null,
                "_source": {
                    "title": "杨过啊",
                    "author": "金庸",
                    "word_count": "1111",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "10",
                "_score": null,
                "_source": {
                    "title": "铁中棠啊",
                    "author": "金庸",
                    "word_count": "1111",
                    "publish_date": "1910-09-20"
                },
                "sort": [
                    -1870819200000
                ]
            }
        ]
    }
}

tip:关键字查询的类型好像text能支持模糊匹配,keyword 不支持。

  • 聚合查询
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数:
    aggs:聚合查询请求关键字
    group_by_word_count:聚合条件(自定义)
    terms:关键词
    filed: 聚合字段
{
	"aggs": {
		"group_by_word_count":{
			"terms": {
				"field":"word_count"
			}
		}
	}
}

返回值:

{
    "took": 2288,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v2.0",
                    "author": "huamanlou",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "李寻欢啊",
                    "author": "古龙",
                    "word_count": "1221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "title": "花满楼啊",
                    "author": "古龙",
                    "word_count": "22",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "title": "丁鹏啊",
                    "author": "金庸",
                    "word_count": "2221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "title": "郭靖啊",
                    "author": "金庸",
                    "word_count": "212",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "12",
                "_score": 1,
                "_source": {
                    "title": "鸠摩智啊",
                    "author": "金庸",
                    "word_count": "44",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v1.0",
                    "author": "luxiaofeng",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "楚留香啊",
                    "author": "古龙",
                    "word_count": "2811",
                    "publish_date": "1990-09-20"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 28,
                    "doc_count": 2
                },
                {
                    "key": 1111,
                    "doc_count": 2
                },
                {
                    "key": 11,
                    "doc_count": 1
                },
                {
                    "key": 22,
                    "doc_count": 1
                },
                {
                    "key": 44,
                    "doc_count": 1
                },
                {
                    "key": 212,
                    "doc_count": 1
                },
                {
                    "key": 1221,
                    "doc_count": 1
                },
                {
                    "key": 2221,
                    "doc_count": 1
                },
                {
                    "key": 2222,
                    "doc_count": 1
                },
                {
                    "key": 2811,
                    "doc_count": 1
                }
            ]
        }
    }
}

key:字数值(统计标准)
doc_count: 满足key的总数

  • 多个聚合条件
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数:
    aggs:聚合查询请求关键字
    group_by_word_count:聚合条件(自定义)
    terms:关键词
    filed: 聚合字段
{
	"aggs": {
		"group_by_word_count":{
			"terms": {
				"field":"word_count"
			}
		},
		"group_by_publish_date":{
			"terms": {
				"field":"publish_date"
			}
		}
	}
}

返回值:
展示了两组聚合数据

{
    "took": 381,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v2.0",
                    "author": "huamanlou",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "李寻欢啊",
                    "author": "古龙",
                    "word_count": "1221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "title": "花满楼啊",
                    "author": "古龙",
                    "word_count": "22",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "title": "丁鹏啊",
                    "author": "金庸",
                    "word_count": "2221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "title": "郭靖啊",
                    "author": "金庸",
                    "word_count": "212",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "12",
                "_score": 1,
                "_source": {
                    "title": "鸠摩智啊",
                    "author": "金庸",
                    "word_count": "44",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v1.0",
                    "author": "luxiaofeng",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "楚留香啊",
                    "author": "古龙",
                    "word_count": "2811",
                    "publish_date": "1990-09-20"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_publish_date": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": -1870819200000,
                    "key_as_string": "1910-09-20 00-00-00",
                    "doc_count": 9
                },
                {
                    "key": 653788800000,
                    "key_as_string": "1990-09-20 00-00-00",
                    "doc_count": 3
                }
            ]
        },
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 28,
                    "doc_count": 2
                },
                {
                    "key": 1111,
                    "doc_count": 2
                },
                {
                    "key": 11,
                    "doc_count": 1
                },
                {
                    "key": 22,
                    "doc_count": 1
                },
                {
                    "key": 44,
                    "doc_count": 1
                },
                {
                    "key": 212,
                    "doc_count": 1
                },
                {
                    "key": 1221,
                    "doc_count": 1
                },
                {
                    "key": 2221,
                    "doc_count": 1
                },
                {
                    "key": 2222,
                    "doc_count": 1
                },
                {
                    "key": 2811,
                    "doc_count": 1
                }
            ]
        }
    }
}
  • 对某个字段进行计算
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数:
    aggs:聚合查询请求关键字
    group_by_word_count:聚合条件(自定义)
    stats:进行计算关键词
    filed: 聚合字段
{
	"aggs": {
		"group_by_word_count":{
			"stats": {
				"field":"word_count"
			}
		}
	}
}

返回值:

{
    "took": 543,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v2.0",
                    "author": "huamanlou",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "陆小凤啊",
                    "author": "古龙",
                    "word_count": "11",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "李寻欢啊",
                    "author": "古龙",
                    "word_count": "1221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "title": "花满楼啊",
                    "author": "古龙",
                    "word_count": "22",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "title": "丁鹏啊",
                    "author": "金庸",
                    "word_count": "2221",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "title": "郭靖啊",
                    "author": "金庸",
                    "word_count": "212",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "12",
                "_score": 1,
                "_source": {
                    "title": "鸠摩智啊",
                    "author": "金庸",
                    "word_count": "44",
                    "publish_date": "1910-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "ekasticsearch v1.0",
                    "author": "luxiaofeng",
                    "word_count": "28",
                    "publish_date": "1990-09-20"
                }
            },
            {
                "_index": "book",
                "_type": "noval",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "楚留香啊",
                    "author": "古龙",
                    "word_count": "2811",
                    "publish_date": "1990-09-20"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_word_count": {
            "count": 12,
            "min": 11,
            "max": 2811,
            "avg": 920.1666666666666,
            "sum": 11042
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31617637/article/details/85105581