elasticsearch查询

(1)简单查询

复制代码
curl -XGET http://127.0.0.1:9201/_search
curl -XGET http://127.0.0.1:9201/test231208/_search
curl -XGET http://127.0.0.1:9201/test231208/_doc/_search
curl -XGET http://127.0.0.1:9201/test231208/_doc/id

(2)match、match_all、multi_match查询,模糊查询,即先分词后查询;match_all查询全部数据;match针对一个field做查询,multi_match针对多个field做查询,任意一个字段符合条件就行

复制代码
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match": {
			"name": "jerry"
		}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match_all": {}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"multi_match": {
			"query": "jerry",
			"fields": [
				"name"
			]
		}
	}
}'

(3)term查询或range查询,精确查询

复制代码
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"term": {
			"age": 2
		}
	}
}'
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"range": {
			"age": {
				"gte": 3,
				"lte": 4
			}
		}
	}
}'

(4)bool查询,一个或多个查询子句的组合,must表示必须匹配(类似与)、should表示选择性匹配(类似或)、must_not表示必须不匹配(类似非)、filter表示过滤条件

复制代码
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"name": "tom"
					}
				},
				{
					"match": {
						"age": 2
					}
				}
			],
			"should": [
				{
					"match": {
						"name": "tom"
					}
				},
				{
					"match": {
						"name": "jerry"
					}
				}
			],
			"must_not": [
				{
					"match": {
						"name": "diana"
					}
				}
			],
			"filter": [
				{
					"range": {
						"age": {
							"gte": 2,
							"lte": 4
						}
					}
				}
			]
		}
	}
}'

(5)查询部分属性字段、分页和排序

复制代码
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"match_all": {}
	},
	"_source": {
		"includes": [
			"age",
			"name"
		]
	},
	"from": 0,
	"size": 10,
	"sort": [
		{
			"age": {
				"order": "asc"
			}
		}
	]
}'

(6)group by分组

复制代码
curl -XGET http://127.0.0.1:9201/test231208/_search -d 
'{
	"query": {
		"range": {
			"gmt_create": {
				"gte": "20230101000000",
				"lte": "20240101000000"
			}
		}
	},
	"from": 0,
	"size": 0,
	"sort": [],
	"aggs": {
		"group_by_key": {
			"terms": {
				"field": "age"
			}
		}
	}
}'

响应体,如下

复制代码
{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "group_by_key": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 2,
                    "doc_count": 1
                },
                {
                    "key": 4,
                    "doc_count": 1
                }
            ]
        }
    }
}
相关推荐
G皮T9 小时前
【Elasticsearch】正排索引、倒排索引(含实战案例)
大数据·elasticsearch·搜索引擎·kibana·倒排索引·搜索·正排索引
G皮T14 小时前
【Elasticsearch】Elasticsearch 近实时高速查询原理
大数据·elasticsearch·搜索引擎·全文检索·倒排索引·搜索·nrt
aini_lovee1 天前
python在容器内克隆拉取git私有仓库
git·python·elasticsearch
在未来等你1 天前
SQL进阶之旅 Day 29:NoSQL结合使用策略
redis·sql·mongodb·elasticsearch·postgresql·nosql·hybrid-database
想躺平的咸鱼干2 天前
Elasticsearch 的自动补全以及RestAPI的使用
java·后端·elasticsearch·中间件·intellij-idea
摇滚侠2 天前
elasticSearch是什么,如何使用,有什么用
大数据·elasticsearch·搜索引擎
亲爱的非洲野猪2 天前
基于ElasticSearch的法律法规检索系统架构实践
大数据·elasticsearch·系统架构
从零开始学习人工智能3 天前
Doris 与 Elasticsearch:谁更适合你的数据分析需求?
大数据·elasticsearch·数据分析
代码搬运媛3 天前
ES Modules 与 CommonJS 的核心区别详解
大数据·elasticsearch·搜索引擎
数据智能老司机3 天前
Elastic 向量搜索实战指南——Elastic中的模型管理与向量相关考量
elasticsearch·搜索引擎·llm