elasticsearch文档查询

1、普通查询

es的文档查询可以在请求时候拼接参数查询,如下:

GET   http://127.0.0.1:9200/students/_search?q=age:24

返回结果如下:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "userId": 1002,
                    "userName": "Tom",
                    "age": 24,
                    "hobby": "游泳,足球"
                }
            }
        ]
    }
}

这种查询的方式,没有考虑查询参数是中文的时候中文的编码问题,所以推荐使用请求体带参数的方式:
如下:

POST    http://127.0.0.1:9200/students/_search

请求Body:

{
    "query": {
        "match": {
            "age": 24
        }
    }
}

返回结果为:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "userId": 1002,
                    "userName": "Tom",
                    "age": 24,
                    "hobby": "游泳,足球"
                }
            }
        ]
    }
}

2、分页查询

POST    http://127.0.0.1:9200/students/_search

请求Body:

{
    "query": {
        "match_all": {}
    },
    "from": 0,
    "size": 2,
    "_source": [
        "userName",
        "hobby"
    ],
    "sort":{
        "userId":{
            "order":"desc"
        }
    }
}

match_all表示全量查询,from表示开始的条数而非页数,size表示每页的条数,_source字段用来指定查询的字段,不需要将所有的字段全部返回,sort字段表示排序,order用来指定升序还是降序
返回结果为:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": null,
                "_source": {
                    "userName": "Nancy",
                    "hobby": "游泳,学习"
                },
                "sort": [
                    1002
                ]
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1001",
                "_score": null,
                "_source": {
                    "userName": "Tom",
                    "hobby": "游泳,足球"
                },
                "sort": [
                    1001
                ]
            }
        ]
    }
}

3、复杂组合查询

POST   http://127.0.0.1:9200/students/_search

请求Body:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "age": 24
                    }
                },
                {
                    "match": {
                        "hobby": "游泳"
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 2,
    "_source": [
        "userName",
        "hobby"
    ],
    "sort": {
        "userId": {
            "order": "desc"
        }
    }
}

bool表示查询条件的意思,must表示必须满足的条件,相当于mysql中查询使用的andshoud相当于mysql中查询使用的or
返回结果为:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": null,
                "_source": {
                    "userName": "Nancy",
                    "hobby": "游泳,学习"
                },
                "sort": [
                    1002
                ]
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1001",
                "_score": null,
                "_source": {
                    "userName": "Tom",
                    "hobby": "游泳,足球"
                },
                "sort": [
                    1001
                ]
            }
        ]
    }
}

4、范围查询

POST   http://127.0.0.1:9200/students/_search

请求Body:

{
    "query": {
        "bool": {
            "filter": {
                "range": {
                    "age": {
                        "gt": 20
                    }
                }
            }
        }
    },
    "from": 0,
    "size": 10,
    "_source": [
        "userName",
        "hobby"
    ],
    "sort": {
        "age": {
            "order": "desc"
        }
    }
}

使用range进行范围查询,gt表示大于
返回结果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1004",
                "_score": null,
                "_source": {
                    "userName": "Bom",
                    "hobby": "学习,羽毛球"
                },
                "sort": [
                    26
                ]
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": null,
                "_source": {
                    "userName": "Nancy",
                    "hobby": "游泳,学习"
                },
                "sort": [
                    24
                ]
            }
        ]
    }
}

5、完全匹配高亮显示

POST   http://127.0.0.1:9200/students/_search

请求Body:

{
    "query": {
        "match_phrase": {
            "hobby": "游泳,学习"
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

match_phrase表示整个短语完全匹配,而不使用类似match那样的全文索引,highlight表示对查询结果fields指定的字段进行高亮显示。
查询结果如下:

{
    "took": 182,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.2392645,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": 2.2392645,
                "_source": {
                    "userId": 1002,
                    "userName": "Nancy",
                    "age": 24,
                    "hobby": "游泳,学习"
                },
                "highlight": {
                    "hobby": [
                        "<em>游</em><em>泳</em>,<em>学</em><em>习</em>"
                    ]
                }
            }
        ]
    }
}

6、聚合操作

POST   http://127.0.0.1:9200/students/_search

请求Body:

{
    "aggs": {
        "age_group": {
            "terms": {
                "field": "age"
            }
        }
    }
}

aggs表示的是聚合操作,age_group无特殊含义,可以随意定义,terms分组操作会对field指定的分组字段进行分组操作。
返回结果:

{
    "took": 123,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "userId": 1002,
                    "userName": "Nancy",
                    "age": 24,
                    "hobby": "游泳,学习"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "userId": 1001,
                    "userName": "Tom",
                    "age": 24,
                    "hobby": "游泳,足球"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1003",
                "_score": 1.0,
                "_source": {
                    "userId": 1003,
                    "userName": "Jack",
                    "age": 22,
                    "hobby": "学习,足球"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "userId": 1004,
                    "userName": "Bom",
                    "age": 26,
                    "hobby": "学习,羽毛球"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "userId": 1005,
                    "userName": "Jop",
                    "age": 23,
                    "hobby": "游泳,羽毛球"
                }
            }
        ]
    },
    "aggregations": {
        "age_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 24,
                    "doc_count": 2
                },
                {
                    "key": 22,
                    "doc_count": 1
                },
                {
                    "key": 23,
                    "doc_count": 1
                },
                {
                    "key": 26,
                    "doc_count": 1
                }
            ]
        }
    }
}

如果不想查看到聚合查询的原始数据,可以加个size的参数,如下:

{
    "aggs": {
        "age_group": {
            "terms": {
                "field": "age"
            }
        }
    },
    "size": 0
}

返回结果如下:

{
    "took": 53,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "age_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 24,
                    "doc_count": 2
                },
                {
                    "key": 22,
                    "doc_count": 1
                },
                {
                    "key": 23,
                    "doc_count": 1
                },
                {
                    "key": 26,
                    "doc_count": 1
                }
            ]
        }
    }
}

聚合比如还可以求平均值,例如:

{
    "aggs": {
        "age_avg": {
            "avg": {
                "field": "age"
            }
        }
    },
    "size": 0
}

返回结果如下:

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "age_avg": {
            "value": 23.8
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值