栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch文档查询

elasticsearch文档查询

elasticsearch文档查询
  • 1、普通查询
  • 2、分页查询
  • 3、复杂组合查询
  • 4、范围查询
  • 5、完全匹配高亮显示
  • 6、聚合操作

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中查询使用的and,shoud相当于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": [
                        ","
                    ]
                }
            }
        ]
    }
}
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
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号