ElasticSearch入门3: 高级查询

尊重原创:https://www.cnblogs.com/liuxiaoming123/p/8124969.html

单字段 模糊匹配查询与精准查询

postman请求

POST     127.0.0.1:9200/book/_search

请求json:

{
   "query":{
           "match":{
               "name":"晓明9"
           }
   }
}

注:match 模糊查询的标识 :查询内容自动拆分成分词来查询  若match 改为 match_phrase :精准查询  具体可以查看 http://www.cnblogs.com/liuxiaoming123/p/8119217.html  

响应结果:

{
    "took": 51,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "name": "晓明1",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 0.28363907,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 0.28363907,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 0.23911436,
                "_source": {
                    "name": "晓明9",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

多字段 模糊匹配查询与精准查询

postman请求URL:

POST  127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "multi_match":{
               "query":"晓明china",
               "fields":["name","country"]
           }
   }
}

注:multi_match为指定多字段匹配

响应结果:

{    "took": 42,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 4,        "max_score": 0.5753642,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "1",                "_score": 0.5753642,                "_source": {                    "name": "晓明1",                    "country": "china1",                    "age": 26,                    "date": "1992-08-08"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "5",                "_score": 0.47000363,                "_source": {                    "name": "晓明",                    "country": "china",                    "age": 26,                    "date": "1992-08-08"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "8",                "_score": 0.47000363,                "_source": {                    "name": "晓明",                    "country": "china",                    "age": 26,                    "date": "1992-08-08"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "9",                "_score": 0.23911436,                "_source": {                    "name": "晓明9",                    "country": "china9",                    "age": 26,                    "date": "1992-08-08"                }            }        ]    }}

语法查询 未指定字段:

postman请求:

POST 127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "query_string":{
               "query":"(ElasticSearch AND 入门) OR SpringBoot"
           }
   }
}

返回结果:

{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.634553,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 2.634553,
                "_source": {
                    "name": "ElasticSearch 入门",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "name": "SpringBoot",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

语法查询 
 指定字段:

postman请求:

POST 127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "query_string":{
               "query":"SpringBoot OR 中国",
               "fields":["name","country"]
           }
   }
}

响应结果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.8630463,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.8630463,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

(结构化数据的查询)

指定字段查询:(term)

postman请求:

POST   127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" :
        {
            "term" : {"name" : "springboot"}
        }
}

响应结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

注:若查询英文时 应全字母小写 精确查询

  若查询中文时 应按单个字来查询

范围查询:

注:json请求字符串中部分字段的含义

  range:范围关键字

  gte 大于等于

  lte  小于等于

  gt 大于

  lt 小于

  now 当前时间

postman请求:

127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" :
        {
            "range" : {
                "date" : {
                        "gte":"2017-01-01",
                        "lte":"now"
                         }
                    }
        }
}

响应结果:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "name": "ElasticSearch 入门",
                    "country": "china9",
                    "age": 28,
                    "date": "2017-08-08"
                }
            }
        ]
    }
}

Filter Context(对数据进行过滤)

postman请求:

POST  127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" : {
            "bool" : {
                "filter" : {
                        "term":{
                        "age":20
                                }
                            }
                        }
              }
}

响应结果:

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 20,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

注: boost 固定响应结果分数的值

postman请求:

POST 127.0.0.1:9200/_search

请求json字符串:

{
    "query" : {
            "constant_score" : {
                "filter" : {
                            "match":{
                                        "name":"晓明"
                                    }
                            },
                            "boost":2
                        }
              }
}

响应结果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 8,
        "successful": 8,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 2,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 2,
                "_source": {
                    "name": "晓明8",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}

should关键词:或的关系

若should改为must 关键词则表示 和的关系

postman请求:

POST 127.0.0.1:9200/_search

请求json字符串:
    1.shuld:
{
    "query" : {
            "bool" : {
                "should" : [
                            {
                                "match":{"name":"springboot"}
                            },
                            {
                                "match":{"country":"中国"}
                            }
                            ]
                        }
              }
}

 2.must:
{
    "query" : {
            "bool" : {
                "must" : [
                            {
                                "match":{"name":"springboot"}
                            },
                            {
                                "match":{"country":"中国"}
                            }
                            ]
                        }
              }
}

3.must filter:
{    "query" : {            "bool" : {                "must" : [                            {                                "match":{"name":"springboot"}                            },                            {                                "match":{"country":"中国"}                            }                            ],                   "filter":[                            {                                "term":{                                    "age":20                                }                            }                                ]                        }              }}

4.must_not:{    "query" : {            "bool" : {                "must_not" : {                                "term":{"age":20}                             }                      }               }}

分类: ElasticSearch

原文地址:https://www.cnblogs.com/xiaohouzai/p/9636439.html

时间: 2024-08-26 13:24:25

ElasticSearch入门3: 高级查询的相关文章

MySQL简单快速入门 (三)高级查询——JEPLUS软件快速开发平台

03.SQL高级查询_分组: 1).需求:一条查询,查询出每种商品的最高价格 2).分组的命令:group by 分组字段 3).实现上例: select category_id,max(price)  from product group by category_id; 查询顺序:先分组,再聚合 4).注意事项: 分组查询的结果最多只能包含:分组列,聚合结果,不能包含其他字段. 5).练习1: 需求:查询每个生产日期的商品的数量是多少? select  proDate,count(*)  fr

[学习ES系列]-4.ElasticSearch基础交互-基础查询与高级查询

基础查询 POST http://127.0.0.1:9200/book/_search 1.简单查询 { "query":{ "match_all":{} } } 2.条件查询 { "query":{ "match":{ "title":"入门到精通" } }, "from":1, "size":5, "sort":{ &qu

Elasticsearch入门教程(五):Elasticsearch查询(一)

原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/79228852 // 准备数据 PUT /shop/goods/1 { "name": "2017新款女装冬季外套ulzzang棉袄中长款棉衣韩版百搭棉服面包服", &quo

Elasticsearch入门教程(六):Elasticsearch查询(二)

原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/79237950 地理坐标点geo-point 地理坐标点:是指地球表面可以用经纬度描述的一个点. 地理坐标点可以用来计算两个坐标间的距离,还可以判断一个坐标是否在一个区域中,或在聚合中. 地理坐标点不能被

Linq——高级查询方法入门

一,Lambda表达式 lambda表达式刚开始用的时候还很不习惯,因为以前用惯了那种先foreach,再逐个判断的麻烦形式,刚开始用lambda都会在脑子里转一下,变成自己让自己舒服的格式,但是写过几行代码后,就会喜欢上这种形式,首先,它比较简洁,其次,和LINQ组合起来用感觉非常贴近SQL: 二,LINQ高级查询内容简介 LINQ的写法有两种,一种是语句形式,一种是方法形式,但是语句形式支持的功能貌似不足方法强大,所以,有些时候写语句形式的LINQ表达式还要加入方法.在编译的时候,也是将语句

ElasticSearch入门 第一篇:Windows下安装ElasticSearch

https://www.elastic.co/downloads/past-releases/elasticsearch-2-4-4 这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ElasticSearch入门 第三篇:索引 ElasticSearch入门 第四篇:使用C#添加和更新文档 ElasticSearch入门 第五篇:使用C#查询文档

Elasticsearch入门教程(一):Elasticsearch及插件安装

原文:Elasticsearch入门教程(一):Elasticsearch及插件安装 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/79194244 分享一个朋友的人工智能教程(请以"右键"->"在新标签页中打开连接"的方式访问).比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 一:安装Elasti

ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套

这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ElasticSearch入门 第三篇:索引 ElasticSearch入门 第四篇:使用C#添加和更新文档 ElasticSearch入门 第五篇:使用C#查询文档 ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套 在ElasticSearch中,使用JSON结构来存储数据,

Mybatis最入门---ResultMaps高级用法(上)

[一步是咫尺,一步即天涯] 接上文,我们基本的单表查询使用上文中的方式已经能够达到目的.但是,我们日常的业务中也存在着多表关联查询,结果是复杂的数据集合等等.本文我们就来介绍ResultMaps的高级用法,本文,我们先介绍基本的概念,具体用法实例在下一篇中专门演示给大家.敬请期待! ------------------------------------------------------------------------------------------------------------