ElasticSearch 查询小结之一

本文介绍了Elasticsearch中多种查询方式的应用,包括简单的inline查询、复杂的查询DSL、match_all、termquery、booleanquery、wildcardquery及rangequery等。通过实际示例详细解释了每种查询的特点和使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 - inline 查询
在Restful API中,连同查询参数一起,指定查询关键字:

curl -XGET 'Http://localhost:9200/ebook/book/_search?q=Elastic&prettry=true 

2 - 查询 DSL
在使用Restful API检索文档的时候,使用复杂的查询条件,比如多条件查询(复合查询),更改积分权重等:

curl -XGET 'http://localhost:9200/ebook/book/_search?pretty=true' -d '{
"query": {
"query_string":{ "query":"title:crime"}
}
}

(以下查询都是基于Kibana提交,省却了curl的一系列参数设置)

2.1 - match_all:
不适用任何索引关键字,查询所有的文档,类似于sql的select *

GET 'ebooks/POST/_search'
{
  "query":{
    "match_all":{}
  }
}

2.2 - term query :

GET ebooks/POST/_search
{
  "query":{
    "term":{
      "content":"fire"
    }
  }
}

下面这个查询,要注意大小写。假如redemption首字大写,那么就会查询不到。而原文中,首字是大写的。初步判断是因为在查询的时候,没有指定一个合适的analyser.

GET ebooks/POST/_search
{
  "query":{
    "term":{
      "content":"redemption"
    }
  }
}

以下查询,首字大写,并没有查获任何的匹配文档。

GET ebooks/POST/_search
{
  "query":{
    "term":{
      "content":"Redemption"
    }
  }
}

尝试着给查询字段增加一个analyzer:

GET ebooks/POST/_search
{
  "query":{
    "term":{
      "content":{
        "value":"Redemption",
        "analyzer":"simple"
      }

    }
  }
}

报错:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[term] query does not support [analyzer]",
        "line": 6,
        "col": 20
      }
    ],
    "type": "parsing_exception",
    "reason": "[term] query does not support [analyzer]",
    "line": 6,
    "col": 20
  },
  "status": 400
}

term是词干的意思,就是已经被提炼和分析过的原生字,可以想象成为原子级别的查询。在term query里面,不需要再做任何的分析,就只能是全字符串匹配。所以碰到这种情况,需要使用分析器的例子,就要使用match查询:

GET ebooks/POST/_search
{
  "query":{
    "match":{
      "content":"Redemption"
      }

    }
  }
}

2.3 boolean query:

boolean查询,条件组合查询。当有多个条件表达式组合时候,可以组合起来判别最终的条件:

,"should":[
        {"match":{"content":"redemption"}}
        ,{"match":{"content":"there"}}
        ]

下面两段脚本,如果没有指定should的minimum_number_should_match,则结果是一样的:
must是在should应用之后,再执行:表示一定要符合的条件; 而should可以根据其中一或者多个匹配表达式,来查找文档的;

GET ebooks/POST/_search 
{
  "query":{
    "bool":{
      "must":[{"match":{"content":"there"}}
              ,{"match":{"content":"redemption"}}]
      ,"should":[
        {"match":{"content":"redemption"}}
        ,{"match":{"content":"there"}}
        ]
    }
  }
}
GET ebooks/POST/_search 
{
  "query":{
    "bool":{
      "must":[{"match":{"content":"there"}}
              ,{"match":{"content":"redemption"}}]

    }
  }
}

除了使用must,还可以使用boolean的minimum_number_should_match

GET ebooks/POST/_search 
{
  "query":{
    "bool":{
      "must":[{"match":{"content":"there"}}
              ,{"match":{"content":"redemption"}}]
      ,"should":[
        {"match":{"content":"redemption"}}
        ,{"match":{"content":"there"}}
        ]
    }
  }
}

2.4 wildcard query

, ? 是两个在查询条件中使用的通配符。 代表了多个字符通配,?仅仅代表一个通配字符

GET ebooks/POST/_search 
{
  "query":{
    "wildcard":{
      "content":{"value":"langol*"}
    }
  }
}

没有想到的是,bool和wildcard能够联合成符合查询。

GET ebooks/POST/_search 
{
  "query":{
    "bool":{
      "must_not":{"wildcard":{
      "content":"langol*"
    }}
    }    
  }
}

2.5 range query

GET ebook/book/_search
{
  "query":{
    "range":{
      "charpter":{
        "gte":0
        ,"lte":5
      }
    }
  }
}

欢迎关注个人微信公众号【有关SQL】

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dbLenis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值