Elasticsearch Search API

前言

前面的文章中主要介绍了Elasticsearch的安装及基本的CRUD操作,在使用Elasticsearch的时候,大部分是使用他的搜索,本次我们就来了解更多搜索的API。

URI Search

这种方式用得不多,一般用得比较多的是Request Body Search。

URI Search可以使用GET,也可以使用POST,基本的语法如下

GET /<target>/_search?xxx
POST /<target>/_search?xxx

一些例子

// 查询的索引是movies,profile=true相当于MySQL的explain,df指定查询的字段,q指定查询的内容,from从第几条开始,size返回多少条,sort指定排序规则(可选值asc desc),timeout指定搜索的超时时间
GET movies/_search?q=2012&df=title&from=0&size=3&sort=movieId:desc&timeout=1s
{
  "profile": "true"
}


// 对q+dp的简化方式,查询title字段,值为2012的文档
GET movies/_search?q=title:2012

// 括号内可以文本会进行分词搜索,默认是OR,可以使用AND,还可以使用“+ -”(“+”要用“%2B”进行转义),“+”表示必须有,“-”必须没有
GET movies/_search?q=title:(beautiful mind)
GET movies/_search?q=title:(beautiful AND mind)
GET movies/_search?q=title:(%2Bbeautiful %2Bmind)

// 范围查询
GET movies/_search?q=movieId:>1900
GET movies/_search?q=movieId:[2 TO 5]

// 正则匹配
GET movies/_search?q=title:beauti*
GET movies/_search?q=title:beau*ful

// 近似查询,“~2”与下文中match_phrase的“slop 2”一致,具体含义参考下文
GET movies/_search?q=title:"Corrupt beautiful"~2

Request Body Search

基本语法

GET movies/_search
{
  "profile": "true",
  "_source": ["movieId", "title","genres"], 
  "sort": [{"movieId": "desc"}],
  "from": 0,
  "size": 3,
  "query": {
    "match_all": {}
  }
}
  • movies 查询的索引
  • profile 相当于MySQL中的explain
  • _source 是要返回的字段
  • sort 指定排序的字段
  • from 从第几条开始,默认是0
  • size 返回多少条数据,默认是10
  • query 指定查询的方式,可选值一般有(match_all, match, match_phrase, query_string, simple_query_string),match_all查询所有

match

GET movies/_search
{
  "profile": "true",
  "_source": ["movieId", "title","genres"], 
  "sort": [{"movieId": "desc"}],
  "from": 0,
  "size": 3,
  "query": {
    "match": {
      "title":{
        "query":"beautiful mind",
        "operator": "or"
      }
    }
  }
}

会进行分词匹配,可以指定operator,可选值(and or not),默认是or

match_phrase

GET movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query":"beautiful mind",
        "slop": 1
      }
    }
  }
}

slop等同于上面的近似查询“~1”。短语匹配,类似SQL中的like,但可以设置slop实现近似匹配,表示移动多少个词可以进行匹配。

比如有文本“beautiful mind people”,搜索短语“people beautiful”需要移动3次才能匹配(beautiful第一次移到mind那个位置,第二次移到people那个位置,第三次移到people的后面);而搜索短语“beautiful people”需要移动1次就能匹配。

query_string

GET movies/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "(beautiful AND mind) OR (beautiful AND people)"
    }
  }
}

query中的语法支持逻辑符合(AND OR NOT)

simple_query_string

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

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

类似query_string,但会忽略错误的语法。

query中不支持“AND OR NOT”,会被当做字符串处理。支持的逻辑处理是“+ | -”,分别代表“AND OR NOT”

可以指定default_operator,支持AND OR。

query中的逻辑处理与default_operator的逻辑符合一起用,会以query中的逻辑处理为准。

参考资料

__EOF__

 


 

欢迎转载,但请注明出处!
欢迎大家一起交流学习!如果有什么疑问,大家可以在评论区一起交流!
如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是我的最大动力!

原文地址:https://www.cnblogs.com/powercto/p/14460810.html