elasticsearch高级查询

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 子条件查询 特定字段查询所指特定值 1.1 Query Context 在查询过程中,除了判断文档是否满足查...
    edwin1993阅读 7,954评论 0 1
  • 查詢分为子条件查詢和复合条件查詢子条件查詢:特定字段查詢所指特定值复合条件查詢:以一定的逻辑组合子条件查詢 一、子...
    aeborah阅读 2,563评论 0 0
  • 子条件查询 子条件查询指特定字段查询所指特定值 全文本查询 全文本查询针对文本类型数据 字段级别查询 针对结构化数...
    bullion阅读 5,480评论 0 0
  • DSL语句查询 查询字符串搜索便于通过命令行完成特定(ad hoc)的搜索,但是它也有局限性(参阅简单搜索章节)。...
    weylau阅读 13,081评论 0 3
  • elasticSearch高级查询 全量查询 条件查询 分页查询 查询排序 过滤字段 组合查询 范围查询 模糊查询...
    辰若月熙阅读 2,523评论 0 0