美文网首页
elasticsearch高级查询

elasticsearch高级查询

作者: 念䋛 | 来源:发表于2022-05-15 14:56 被阅读0次

elasticsearch7.8.0高级查询

数据准备

批量插入数据
127.0.0.1:9200/student/_doc/_bulk post请求
{"index": {"_id": "1"}}
{"name":"zhangsan","nickname":"zhangsan", "sex":"男", "age":30}
{"index": {"_id": "2"}}
{"name":"lisi","nickname":"lisi", "sex":"男", "age":20}
{"index": {"_id": "3"}}
{"name":"wangwu", "nickname":"wangwu", "sex":"女", "age":40}
 {"index": {"_id": "4"}}
{"name":"zhangsan1","nickname":"zhangsan1", "sex":"女", "age":50}
{"index": {"_id": "5"}}
{"name":"zhangsan2","nickname":"zhangsan2", "sex":"女", "age":300}

查询所有

http://127.0.0.1:9200/student/_search
body
{
    "query":{
        "match_all":{} //查询所有
    }
}
结果解析
{
    "took【查询花费时间,单位毫秒】": 1116,
    "timed_out【是否超时】": false,
    "_shards【分片信息】": {
        "total【总数】": 1,
        "successful【成功】": 1,
        "skipped【忽略】": 0,
        "failed【失败】": 0
    },
    "hits【搜索命中结果】": {
        "total"【搜索条件匹配的文档总数】: {
            "value"【总命中计数的值】: 3,
            "relation"【计数规则】: "eq"#eq 表示计数准确, gte 表示计数不准确
        },
        "max_score【匹配度分值】": 1.0,
        "hits【命中结果集合】": [。。。
        }
    ]
}
}

匹配查询

http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    }
}

字段匹配查询

http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "multi_match": { //多个匹配
            "query":"zhangsan", //值为zhangsan
            "fields": ["name","nickname"] //两个字段只要一个满足zhangsan即可
        }
    }
}

分词查找

127.0.0.1:9200/shopping/_search
{
    "query":{
        "match":{ //match的条件可以被分词
            "category":"小华" //category映射类型为text,可以被分词
        }
    }
}
查询的结果包括了  category包括了 小米和华为

关键字精确查询

term 查询,精确的关键词匹配查询,不对查询条件进行分词
http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "term": { //精确查找,不分词
            "name": {
                "value": "zhangsan"
            }
        }
    }
}

多关键字精确查询

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。
如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于 mysql 的 in
http://127.0.0.1:9200/student/_search
{
    "query":{
        "terms":{
            "name":["zhangsan","lisi"] 
        }
    }
}

指定查询字段

默认情况下,Elasticsearch 在搜索的结果中,会把文档中保存在_source 的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加_source 的过滤
http://127.0.0.1:9200/student/_search
{
    "_source": [
        "name",
        "nickname"
    ],
    "query": {
        "terms": {
            "nickname": [
                "zhangsan"
            ]
        }
    }
}

过滤字段

includes:来指定想要显示的字段
excludes:来指定不想要显示的字段
http://127.0.0.1:9200/student/_search
body
{
    "_source": {
        "includes": [
            "name",
            "nickname"
        ]
    },
    "query": {
        "terms": {
            "nickname": [
                "zhangsan"
            ]
        }
    }
}

组合查询

`bool`把各种其它查询通过`must`(必须 )、`must_not`(必须不)、`should`(应该)的方
式进行组合
http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "zhangsan"
                    }
                }
            ],
            "must_not": [
                {
                    "match": {
                        "age": "40"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "sex": "男" //可以不满足,如果满足了打分高
                    }
                }
            ]
        }
    }
}
查询的结果中如果出现
failed to create query: Cannot search on field [age] since it is not indexed.
是在创建age映射的时候,index为false
这时候需要修改age可以被索引,但是elasticsearch不支持修改索引
曲线救国

修改索引映射

创建新的索引
http://127.0.0.1:9200/student2
创建映射
127.0.0.1:9200/student2/_mapping
body
{
    "properties": {
        "name": {
            "type": "text", //可以分词查询
            "index": true   //是否索引
        },
        "sex": {
            "type": "text",
            "index": true
        },
        "age": {
            "type": "long",
            "index": true
        }
    }
}
将student的数据复制到student2中
student中 sex和age index为false 
student2中sex和age index为true
数据复制的时候,会将sex和age创建索引
127.0.0.1:9200/_reindex
body
{
    "source": {
        "index": "student2"
    },
    "dest": {
        "index": "student"
    }
}
删除student索引
http://127.0.0.1:9200/student delete方法
创建student索引和映射,并将sutdent2的数据复制到student索引上这里就不展示了

范围查询

http://127.0.0.1:9200/student/_search
{
    "query": {
        "range": {
            "age": {
                "gte": 30,
                "lte": 35
            }
        }
    }
}
image.png

模糊查询

http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "fuzzy": {
            "name": {
                "value": "zhangsan",
                "fuzziness": 1 //代表name值修改一次之后为zhangsan
                                       //zhangsn sn修改一次为zhangsan 可以被查询到
            }
        }
    }
}
更多可参考:
https://wenku.baidu.com/view/b08b310e677d27284b73f242336c1eb91a373379.html

排序查询

http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    },
    "sort": [ //数组,可以多个字段排序  
        {
            "age": {
                "order": "desc"
            }
        }
    ]
}

高亮查询

http://127.0.0.1:9200/student/_search
body
{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    },
    "highlight": {
        "pre_tags": "<font color='red'>", //将查询到的结果用标签装饰
        "post_tags": "</font>",
        "fields": {
            "name": {}
        }
    }
}

postman-json文件
git@gitee.com:zhangjijige/file.git

相关文章

网友评论

      本文标题:elasticsearch高级查询

      本文链接:https://www.haomeiwen.com/subject/gxfmurtx.html