4.elasticsearch中查询条件

发布时间 2024-01-05 10:41:10作者: 赛博朋克V

一、URI查询

(这里在kibana中可以自己用用,实际上,下文中的DSL查询语句作用会更大些)
通过在URI后面添加参数,实现一些简单条件的查询

  • q指定查询语句,使用Query String Syntax
  • df指定默认字段,不指定查询所有字段
  • sort排序/from和size用于分页
  • profile可以用来查看是如何被执行的
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
    "profile":true
}

指定字段、泛查询

GET /movies/_search?q=2012&df=title
{
    "profile":"true"
}


#泛查询,正对_all,所有字段
GET /movies/_search?q=2012
{
    "profile":"true"
}

分组和phrase

分组查询需要使用(),将查询条件括起来,里面字段默认是or的关系,查出来的文档有其一个就会被查出来

phrase查询需要使用"",里面的关系是and,表示查出来的文档都拥有里面的词,并且按照词组的顺序排列

#分组查询
GET /movies/_search?q=title:(Beautiful Mind)
{
    "profile":"true"
}

#使用引号,Phrase查询
GET /movies/_search?q=title:"Beautiful Mind"
{
    "profile":"true"
}

Bool条件

在term查询中添加指定bool条件

  • 使用大写AND/OR/NOT

  • &&/||/!

#分组,Bool查询默认是or的关系
GET /movies/_search?q=title:(Beautiful Mind)
{
    "profile":"true"
}

#布尔操作符
GET /movies/_search?q=title:(Beautiful AND Mind)
{
    "profile":"true"
}

GET /movies/_search?q=title:(Beautiful NOT Mind)
{
    "profile":"true"
}

must条件

在term查询中可添加must和must_not条件

  • +表示must
  • -表述must_not
GET /movies/_search?q=title:(+Beautiful -Mind)

范围查询

  • []闭区间
  • {}开区间
#范围查询 ,区间写法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018]

通配符查询

  • ?代表一个字符,*代表0或多个
GET /movies/_search?q=title:(beautifu? AND y*)

正则表达式

GET /movies/_search?q=title:[bt]oy

模糊匹配与近似查询

GET /movies/_search?q=title:befutifl~1
GET /movies/_search?q=title:"lord rings"~2

二、RequestBody&DSL

DSL:Query Domain Search Language

ignore_unavailable

ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错

GET /movies,404_idx/_search?ignore_unavailable=true
{
  "profile": true,
    "query": {
        "match_all": {}
    }
}

profile

返回结果中添加此次查询分词等细节

GET /movies,404_idx/_search?ignore_unavailable=true
{
  "profile": true,
    "query": {
        "match_all": {}
    }
}

explain

返回每个文档结果中添加此次查询算分的细节

GET /movies,404_idx/_search?ignore_unavailable=true
{
  "explain": true,
    "query": {
        "match_all": {}
    }
}

query

查询条件

GET /movies,404_idx/_search?ignore_unavailable=true
{
  "profile": true,
    "query": {
        "match_all": {}
    }
}

from、size

分页,from:偏移量,size:每页大小

GET /kibana_sample_data_ecommerce/_search
{
  "from":10,
  "size":20,
  "query":{
    "match_all": {}
  }
}

sort

#对日期排序
GET kibana_sample_data_ecommerce/_search
{
  "sort":[{"order_date":"desc"}],
  "query":{
    "match_all": {}
  }

}

_source

用来过滤返回结果中需要显示的字段

GET kibana_sample_data_ecommerce/_search
{
  "_source":["order_date"],
  "query":{
    "match_all": {}
  }
}

script_fields

脚本字段,用来生产一个新的返回字段,生成规则写在script中

#脚本字段
GET kibana_sample_data_ecommerce/_search
{
  "script_fields": {
    "new_field": {
      "script": {
        "lang": "painless",
        "source": "doc['order_date'].value+'hello'"
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

match

匹配查询,如果此字段设置了分词,会分词查询

GET movies/_search
{
  "query": {
    "match": {
      "title": "last christmas"
    }
  }
}
#operator可以修饰查询的条件
GET movies/_search
{
  "query": {
    "match": {
      "title": {
        "query": "last christmas",
        "operator": "and"
      }
    }
  }
}

match_phrase

短语查询,不会分词查询

GET movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love"
      }
    }
  }
}
#slop参数可以设置查询短语的顺序,1代表单词移动一位还能和所查的短语匹配就命中
#one two love会被查出来
GET movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love",
        "slop": 1
      }
    }
  }
}

term

查询的字段不论是text还是keyword,不会将输入的文本进行分词处理
但是目标字段可能会被分词,所以会导致查不到想要的结果,此时建议用keyword类型的字段查询

#title的内容是"last christmas"
#如果title是text,那么会查不到
#如果title是keyword,会查到
GET movies/_search
{
  "query": {
    "trem": {
      "title": "last christmas"
    }
  }
}

terms

查询字段内包含多个关键词的文档

GET movies/_search
{
  "query": {
    "trems": {
      "title": ["last", "christmas"]
    }
  }
}

multi_match

一个字符串,在多个字段中查询

GET blogs/_search
{
  "query": {
    "multi_match": {
      "type": "best_fields",
      "query": "Quick pets",
      "fields": ["title","body"],
      "tie_breaker": 0.2,
      "minimum_should_match": "20%"
    }
  }
}



GET books/_search
{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": "*_title"
    }
}


GET books/_search
{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": [ "*_title", "chapter_title^2" ]
    }
}

query String

实现多字符串,多字段查询

GET users/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "Ruan AND Yiming"
    }
  }
}

GET users/_search
{
  "query": {
    "query_string": {
      "fields":["name","about"],
      "query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
    }
  }
}

Simple query String

  • 类似Query String,但是会忽略错误的语法,同时只支持部分查询语法
  • 不支持 AND OR NOT 会当作字符串处理
  • Term之间默认的关系是OR,可以指定default_operate
  • 支持部分逻辑
    • +替代AND
    • |替代OR
    • -替代NOT
GET /movies/_search
{
    "profile":true,
    "query":{
        "simple_query_string":{
            "query":"Beautiful +mind",
            "fields":["title"],
            "default_operator": "AND"
        }
    }
}

GET /movies/_search
{
    "profile":true,
    "query":{
        "simple_query_string":{
            "query":"Beautiful +mind",
            "fields":["title"]
        }
    }
}

bool 查询

它是一种嵌套结构,里面可以嵌套上述各种查询逻辑,它本身包含三种逻辑结构。must、should、must_not

GET index /_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "title": "Search"
                    }
                },
                {
                    "match": {
                        "content": "Elasticsearch"
                    }
                }
            ],
            "should": [
                {
                    "term": {
                        "status": "published"
                    }
                },
                {
                    "range": {
                        "publish_date": {
                            "gte": "2015-01-01"
                        }
                    }
                }
            ],
            "must_not": [
                {
                    "exists": {
                        "field":"value"
                    }
                }
            ]
        }
    }
}

filter

给查询添加过滤条件,过滤结构化数据

  • 好处是可以把查询结果添加到缓存中
  • 还有一个作用是过滤的结果不会算分
GET index /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
      "filter": [ 
        { "term":  { "status": "published" }},
        { "range": { "publish_date": { "gte": "2015-01-01" }}}
      ]
    }
  }
}