【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(二)

简介: 【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(二)

match_all

match_all的查询方式简单粗暴,就是匹配所有,不需要传递任何参数:

Post:localhost:9200/tml-userinfo/_doc/_search

请求body

{
    "query": {
        "match_all": {
        }
    }
}

返回结果也是全部的12条数据

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 12,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "7",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "女",
                    "name": "李小美"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "森小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "森小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "李小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "10",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "女",
                    "name": "李小玲"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "12",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "李小辰"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "森小辰"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "女",
                    "name": "森小美"
                }
            }
        ]
    }
}

match_phrase

match_phrase属于短语匹配,能保证分词间的邻近关系,相当于对文档的关键词进行重组以匹配查询内容,对于匹配了短语"森 小 林"的文档,下面的条件必须为true:

  • 森 、小、 林必须全部出现在某个字段中
  • 的位置必须比的位置大1
  • 的位置必须比的位置大2

我们来尝试下对姓名进行检索,请求头和上边完全一样,就不再赘述,直接看请求体,先来看一个不按顺序的

{
    "query": {
        "match_phrase": {
            "name": "森林小"
        }
    }
}

没有返回任何数据

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

再来看一个正确按顺序的:

{
    "query": {
        "match_phrase": {
            "name": "森小林"
        }
    }
}

返回了我们对应的文档:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.3487744,
        "hits": [
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "2",
                "_score": 2.3487744,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "森小林"
                }
            }
        ]
    }
}

muti_match

muti_match标识了只要有一个字段里有匹配短语的词就返回,我们试着在name和sex里找,只要包含的我们就返回该数据,为了验证该特性,我们特意插入一条数据

这样,即使性别为女,如果能返回该数据,说明在两个字段都检索了

{
    "query": {
        "multi_match": {
             "query": "男",
             "fields":  ["name","sex"]  
        }
    }
}

查看返回结果,我们可以看到这条数据返回了:

{
    "took": 114,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 8,
            "relation": "eq"
        },
        "max_score": 0.9808291,
        "hits": [
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "5",
                "_score": 0.9808291,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "13",
                "_score": 0.9808291,
                "_source": {
                    "age": 18,
                    "sex": "女",
                    "name": "李小男"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.6931471,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "森小辰"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "6",
                "_score": 0.6931471,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.44183272,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "森小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.44183272,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "森小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.44183272,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "李小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "12",
                "_score": 0.44183272,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "李小辰"
                }
            }
        ]
    }
}

range

range查询,顾明思意就是范围查询,例如我们这里要查询年龄在19到28的人的数据:

{
    "query": {
        "range": {
             "age" : {
                        "gte" : 19,
                        "lt"  : 29
                    }
        }
    }
}

返回结果为28岁的员工信息:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "森小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "11",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "女",
                    "name": "森小捷"
                }
            }
        ]
    }
}

这里需要解释下:

range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:

  • gt: > 大于(greater than)
  • lt: < 小于(less than)
  • gte: >= 大于或等于(greater than or equal to)
  • lte: <= 小于或等于(less than or equal to)

可以依据自己的需求自由进行组合。

exists

exists允许你过滤文档,只查找那些在特定字段有值的文档,无论其值是多少,为了验证,需要注意,这里的有值即使是空值也算有值,只要不是null,我们新建一个性别为空值的数据【14】,和一个性别为null的数据【15】

可以看到无该字段:

然后进行查询:

{
    "query": {
         "exists": {
            "field": "sex"
          }
    }
}

命中14条,第15条没有命中

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 14,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "李小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "7",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "女",
                    "name": "李小美"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "13",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "女",
                    "name": "李小男"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "age": 28,
                    "sex": "男",
                    "name": "森小林"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "森小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "age": 8,
                    "sex": "男",
                    "name": "李小贤"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "10",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "女",
                    "name": "李小玲"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "12",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "李小辰"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "14",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "",
                    "name": "李小男"
                }
            },
            {
                "_index": "tml-userinfo",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "age": 18,
                    "sex": "男",
                    "name": "森小辰"
                }
            }
        ]
    }
}


相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
相关文章
|
存储 Java API
Elasticsearch 7.8.0从入门到精通
这篇文章详细介绍了Elasticsearch 7.8.0的安装、核心概念(如正排索引和倒排索引)、RESTful风格、各种索引和文档操作、条件查询、聚合查询以及在Spring Boot中整合Elasticsearch的步骤和示例。
629 1
Elasticsearch 7.8.0从入门到精通
|
10月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
559 20
|
12月前
|
存储 运维 监控
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
中信银行信用卡中心每日新增日志数据 140 亿条(80TB),全量归档日志量超 40PB,早期基于 Elasticsearch 构建的日志云平台,面临存储成本高、实时写入性能差、文本检索慢以及日志分析能力不足等问题。因此使用 Apache Doris 替换 Elasticsearch,实现资源投入降低 50%、查询速度提升 2~4 倍,同时显著提高了运维效率。
730 3
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
|
数据采集 人工智能 运维
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
本文介绍了阿里云 Elasticsearch 推出的创新型 AI 搜索方案
736 3
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
|
数据采集 人工智能 运维
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
本文介绍了阿里云 Elasticsearch 推出的创新型 AI 搜索方案。
919 5
ELK 圣经:Elasticsearch、Logstash、Kibana 从入门到精通
ELK是一套强大的日志管理和分析工具,广泛应用于日志监控、故障排查、业务分析等场景。本文档将详细介绍ELK的各个组件及其配置方法,帮助读者从零开始掌握ELK的使用。
|
存储 JSON 监控
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
904 4
|
自然语言处理 搜索推荐 Java
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(一)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图
340 0
|
存储 自然语言处理 搜索推荐
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
323 0
|
消息中间件 监控 关系型数据库
MySQL数据实时同步到Elasticsearch:技术深度解析与实践分享
在当今的数据驱动时代,实时数据同步成为许多应用系统的核心需求之一。MySQL作为关系型数据库的代表,以其强大的事务处理能力和数据完整性保障,广泛应用于各种业务场景中。然而,随着数据量的增长和查询复杂度的提升,单一依赖MySQL进行高效的数据检索和分析变得日益困难。这时,Elasticsearch(简称ES)以其卓越的搜索性能、灵活的数据模式以及强大的可扩展性,成为处理复杂查询需求的理想选择。本文将深入探讨MySQL数据实时同步到Elasticsearch的技术实现与最佳实践。
679 0

热门文章

最新文章