Elasticsearch系列-搜索操作

指定查询的索引

语法范围
/_search集群上所有索引
/index1/_searchindex1
/index1,index2/_searchindex1和index2
/index*/_search以index开头的索引

URl查询

查询参数:

  • q: 指定查询语句,使用 Query String Syntax
  • df: 默认字段,不指定时,会对所有字段进行查询
  • sort:排序
  • from 和size: 用于分页
  • profile: 可以查看查询是如何执行的

Query String Syntax

  • 指定字段:q=title:2012
  • 泛查询:q=2012
  • Term&Phrase:
    • Beautiful Mind 等效于Beautiful OR Mind
    • “Beautiful Mind” 等效于Beautiful AND Mind。Phrase查询,要求前后顺序保持一致
  • 分组与引号:
    • title:(Beautiful AND Mind)
    • title=“Beautiful Mind”
  • 布尔操作:
    • AND OR NOT 或者 && || !
      • 必须大写
      • title:(matrix NOT reloaded)
  • 分组
    • +表示must
    • -表示must_not
    • title:(+matrix -reloaded)
  • 范围查询:区间表示:[]闭区间,{}开区间
    • year:{2019 TO 2018]
    • year:[* TO 2018]
  • 算数符号:> >= < <= !
  • 通配符查询:?代表一个字符,*代表0或多个字符
    • title:mi?d
    • title:be*
  • 正则表达式:title:[bt]oy
  • 模糊匹配与近似查询:
    • title:befutifl~1
    • title:“lord rings”~2

相关示例(通过kibana开发工具):

//全文搜索,只返回前10条数据
GET /movies/_search


//全文搜索,会匹配所有字段
GET /movies/_search?q=2012
{
  "profile": "true"
}

//全文搜索,会匹配所有字段
GET /movies/_search?q="2012"
{
  "profile": "true"
}

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

//匹配title字段是 "2012 2011" 不会做分词匹配
GET /movies/_search?q=title:"2012 2011"
{
  "profile": "true"
}

//匹配title字段是2012或者2011的数据  OR必须大写
GET /movies/_search?q=title:(2012 OR 2011)
{
  "profile": "true"
}

//匹配title字段是 2012并且包含2011 AND必须大写
GET /movies/_search?q=title:(2012 AND 2011)
{
  "profile": "true"
}

//匹配title字段是2012 和 所有字段匹配2011
GET /movies/_search?q=title:2012 2011
{
  "profile": "true"
}

//通配符匹配ti开头的字段是2012
GET /movies/_search?q=2012&df=ti*
{
  "profile": "true"
}

//通配符匹配title字段是201开头
GET /movies/_search?q=title:201*
{
  "profile": "true"
}

//通配符匹配ti开头的字段是2012 通配符前面添加 "\"做转义
GET /movies/_search?q=ti\*:2012
{
  "profile": "true"
}

//匹配title字段不为null的数据
GET /movies/_search?q=_exists_:title
{
  "profile": "true"
}

//匹配title字段为kony的数据
GET /movies/_search?q=title:kony
{
  "profile": "true"
}

//匹配title字段为kony的数据 并允许有两位字符误差 "~" 默认运行两位误差
GET /movies/_search?q=title:kony~
{
  "profile": "true"
}

//匹配title字段为koey的数据 并允许有一位字符误差 "~" 默认运行两位误差
GET /movies/_search?q=title:koey~1
{
  "profile": "true"
}

//匹配year >= 2011 并且 <=2013的数据
GET /movies/_search?q=year:(>=2011 AND <=2013)
{
  "profile": "true"
}

//匹配year 2012以上的信息
GET /movies/_search?q=year:[2012 TO *]
{
  "profile": "true"
}

//匹配year >= 2011 并且 <=2013的数据
GET /movies/_search?q=year:[2012 TO 2014]
{
  "profile": "true"
}

//title字段必须含有matrix 并且必须不包含reloaded
GET /movies/_search?q=title:(+matrix AND -reloaded)
{
  "profile": "true"
}

//多字段查询
GET /movies/_search?q=(title:bingo OR id:150092)
{
  "profile": "true"
}

//多字段查询
GET /movies/_search?q=(title:bingo AND id:150092)
{
  "profile": "true"
}

Request Body查询

sort排序

GET /movies/_search
{
  "sort": [
    {"year": "desc"},
    {"title.keyword": "asc"}
  ]
}

from,size分页

from从0开始,size默认10条

GET /movies/_search
{
  "from": 0,
  "size": 20
}

_source 过滤字段

GET /movies/_search
{
  "_source": "title"
}
//source只返回title,year字段
GET /movies/_search
{
  "_source": ["title","year"]
}

match表达式

//查询title字段同时含有charley moon值
GET /movies/_search
{
  "profile": "true", 
  "query": {
    "match": {
      "title": {
        "query": "charley moon",
        "operator": "and"
      }
    }
  }
}

match_phrase 分词表达式

GET /movies/_search
{
  "from": 0, 
  "size": 20, 
  "profile": "true",
  "query": {
    "match_phrase": {
      "title": {
        "query": "mind murder",
        "slop": 1
      }
    }
  }
}

query string表达式

query 字段赋值参照Query String Syntax

GET /movies/_search
{
  "profile": "true",
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "charley moon",
      "default_operator": "AND"
    }
  }
}

GET /movies/_search
{
  "profile": "true",
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "charley AND moon"
    }
  }
}

simple query string

query 字段赋值参照Query String Syntax

GET /movies/_search
{
  "profile": "true",
  "query": {
    "simple_query_string": {
      "query": "charley moon",
      "fields": ["title"],
      "default_operator": "AND"
    }
  }
}

GET /movies/_search
{
  "profile": "true",
  "query": {
    "simple_query_string": {
      "query": "+charley -moon",
      "fields": ["title"],
      "default_operator": "AND"
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Layne_lei

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

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

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

打赏作者

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

抵扣说明:

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

余额充值