ES API 语法学习
//JSON格式中,字符串需要用双引号包围,而数字则不需要
//-s:以静默模式运行,不显示进度条和其他信息
19.多条件查询文档内容
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query": {
"bool": {
"must": [
{
"match": {
"category": "小米" //匹配小米
}
},
{
"match": {
"price": 3999.0 //匹配价格
}
}
]
}
}
}'
同时满足两条结果 符合条件的有14个文档
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 14,
"relation" : "eq"
},
"max_score" : 1.0678031,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "m8NgK5MB5s028Is5u9La",
"_score" : 1.0678031,
"_source" : {
"title" : "小米手机",
"category" : "小米",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
满足不同的条件匹配
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d '{
"query": {
"bool": {
"should": [ //或
{
"match": {
"category": "小米" //小米
}
},
{
"match": {
"category": "华为" //华为
}
}
]
}
}
}' |jq -r .hits.total.value // 筛选value结果
16 //总共返回16条数据包含 "category": "华为"
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 16,
"relation": "eq"
},
"max_score": 3.8338451,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "q8M4NZMB5s028Is5aNKX",
"_score": 3.8338451,
"_source": {
"title": "小米手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
20.范围查询文档内容
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d '{
"query": {
"bool": {
"should": [ //或
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
],
"filter" : { //过滤
"range" : { //范围
"price" : { //价格
"gt" : 5000 //大于5000
}
}
}
}
}
}'
满足于华为和小米 大于5000的结果为0
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
....
21.全文检索 根据分词去倒排查询内容
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : { //匹配
"category": "华为" //分词
}
}
}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2, //匹配到两条数据内容
"relation" : "eq"
},
"max_score" : 3.8338451,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "q8M4NZMB5s028Is5aNKX",
"_score" : 3.8338451,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "rMM4NZMB5s028Is5etL6",
"_score" : 3.8338451,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
}
]
}
}
分词查询
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : {
"category": "华" //分词 倒排查询
}
}
}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2, //匹配两条数据
"relation" : "eq"
},
"max_score" : 1.9169226,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "q8M4NZMB5s028Is5aNKX",
"_score" : 1.9169226,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
........
可以同时匹配 两个分词
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : {
"category": "小华" //小为一个分词 华一个分词 去倒排查询结果
}
}
}'
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16, //16条数据
"relation" : "eq"
},
"max_score" : 1.9169226,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "q8M4NZMB5s028Is5aNKX",
"_score" : 1.9169226,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "rMM4NZMB5s028Is5etL6",
"_score" : 1.9169226,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "m8NgK5MB5s028Is5u9La",
"_score" : 0.15906468,
"_source" : {
"title" : "小米手机",
"category" : "小米",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
.....
22.完全匹配 完全命中内容
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match_phrase" : { //完全匹配
"category" : "小米"
}
}
}'
{
"took" : 96,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16, //返回16条数据
"relation" : "eq"
},
"max_score" : 0.059705928,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "m8NgK5MB5s028Is5u9La",
"_score" : 0.059705928,
"_source" : {
"title" : "小米手机",
"category" : "小米",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
},
......
完全匹配不存在的内容
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match_phrase" : {
"category" : "小华"
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0, //返回结果为0
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ] //结果内容为空
}
}
23.查询内容 高亮显示
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match_phrase" : {
"category" : "华为" //完全匹配华为
}
},
"highlight" : { //高亮显示
"fields" : { //筛选字段
"category" : {}
}
}
}'
{
"took" : 722,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2, //两条数据
"relation" : "eq"
},
"max_score" : 3.8338451,
"hits" : [
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "q8M4NZMB5s028Is5aNKX",
"_score" : 3.8338451,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
},
"highlight" : {
"category" : [
"<em>华</em><em>为</em>" //高亮显示
]
}
},
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "rMM4NZMB5s028Is5etL6",
"_score" : 3.8338451,
"_source" : {
"title" : "小米手机",
"category" : "华为",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
},
"highlight" : {
"category" : [
"<em>华</em><em>为</em>"
]
}
}
]
}
}
......
24.聚合数据汇总 查询
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"aggs": { //聚合查询部分
"price_group": { //自定义聚合的名称
"terms": { //分组查询
"field": "price" //分组字段
}
}
}
}'
返回结果带有原来的文档内容
{
"_index" : "shopping",
"_type" : "_doc",
"_id" : "pcNVMJMB5s028Is5PdIA",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"category" : "小米",
"images" : "http://www.gulixueyuan.com/xm.jpg",
"price" : 3999.0
}
}
]
},
"aggregations" : {
"price_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 3999.0, //值
"doc_count" : 16 //16条数据命中
}
]
}
}
}
返回内容过滤 只显示数据汇总值
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"aggs": {
"price_group": { //分组名称
"terms": { //分组
"field": "price"
}
}
},
"size" : 0 //返回文档内容为0
}'
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16, //返回结果
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ] //空的内容
},
"aggregations" : {
"price_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 3999.0, //值
"doc_count" : 16 //16条数据
}
]
}
}
}
汇总数据 取平均值
curl -s -XGET "http://localhost:9200/shopping/_search?pretty" -H "Content-type: application/json" -d \
'{
"aggs": {
"price_avg": {
"avg": { //取平均值
"field": "price"
}
}
},
"size" : 0
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16, //返回结果
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ] //返回内容
},
"aggregations" : {
"price_avg" : {
"value" : 3999.0 //最终平均值
}
}
}
25. 索引映射关系
curl -s -XPUT "http://localhost:9200/user?pretty"
配置映射关系
curl -s -XPUT "http://localhost:9200/user/_mapping?pretty" -H "Content-type: application/json" -d \
'{
"properties" : { //属性
"name" : {
"type" : "text", //全文搜索
"index" : true //索引可以被被查询
},
"sex" : {
"type" : "keyword", //关键字需要完全匹配
"index" : true //索引可以被查询
},
"tel" : {
"type" : "keyword", //关键字需要完全匹配
"index" : false //字段不会被索引,无法通过查询搜索到
}
}
}'
创建文档内容
curl -s -XPUT "http://localhost:9200/user/_create/1001?pretty" -H "Content-type: application/json" -d \
'{
"name" : "小米",
"sex" : "男的",
"tel" : "1111"
}'
全文检索内容
curl -s -XGET "http://localhost:9200/user/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : {
"name" : "小" //分词查询
}
}
}'
.......
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1001",
"_score" : 0.2876821,
"_source" : {
"name" : "小米", //命中了
"sex" : "男的",
"tel" : "1111"
}
}
]
}
}
curl -s -XGET "http://localhost:9200/user/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : {
"sex" : "男" //keyword 类型
}
}
}'
......
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ] //数据为空 需要完全匹配关键字
}
}
curl -s -XGET "http://localhost:9200/user/_search?pretty" -H "Content-type: application/json" -d \
'{
"query" : {
"match" : {
"tel" : "1111" //索引不能被查询
}
}
}'
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: Cannot search on field [tel] since it is not indexed.",
"index_uuid" : "s5BR_OilSKeiHyC2PsK2cA",
"index" : "user"
.....
今日总结
1.多条件查询、范围查询
2.全文检索、完全匹配、高亮查询
3.数据汇总、索引映射关系