Elastic Search RUL Search

本贴最后更新于 370 天前,其中的信息可能已经沧海桑田

Url Query 常用参数:
1.简单查询

example:

PUT test_search_index
{
  "settings": {
    "index": {
      "number_of_shards": "1"
    }
  }
}

PUT test_search_index/doc/_bulk
{"index":{"_id":"1"}}
{"username":"alfred way","job":"java engineer","age":18,"birth":"1990-01-02","isMarried":false}
{"index":{"_id":"2"}}
{"username":"alfred","job":"java senior engineer and java specialist", "age":28,"birth":"1980-05-07","isMarried":true}
{"index":{"_id":"3"}}
{"username":"lee","job":"java and ruby engineer","age":22,"birth":"1985-08-07","isMarried":false}
{"index":{"_id":"4"}}
{"username":"alfred junior way","job":"ruby engineer","age":23,"birth":"1989-08-07","isMarred":false}
GET /my_index/_search?q=alfred&df=user&sort=age:asc&from=4&size=10&timeout=1s
2.过滤查询

-term 与 phrase

alfred way 等价于 alfred OR way, OR 要大写
"alfred way" 短语查询

-泛查询

alfred 等价于在所有字段中匹配 term

-指定字段

name:alfred

# 检索所有字段包含alfred
GET test_search_index/_search?q=alfred
{
  "profile":true
}

# 检索username包含alfred
GET test_search_index/_search?q=username:alfred

# 检索username包含alfred或者所有字段中包含way
GET test_search_index/_search?q=username:alfred way

# 检索username包含alfred或者way
GET test_search_index/_search?q=username:(alfred way)

# 检索username包含alfred way词汇
GET test_search_index/_search?q=username:"alfred way"

# 检索username包含alfred,并且age大于26
GET test_search_index/_search?q=username:alfred age:>26

# 检索birth大于1980且小于1990
GET test_search_index/_search?q=birth:(>1980 AND <1990)
3.分组查询

Group 分组设定,使用括号指定匹配规则

(quick OR brown) AND for
status:(active OR pending)title:(full text search)

####### 4.逻辑运算符

-布尔操作符

# 检索username包含alfred或者所有字段中包含way 
GET test_search_index/_search?q=username:alfred AND way

# 检索username包含alfred和username包含way 
GET test_search_index/_search?q=username:(alfred AND way)

# 检索username包含alfred或者username包含way 
GET test_search_index/_search?q=username:(alfred way)
GET test_search_index/_search?q=username:(alfred OR way)

# 检索username包含alfred且不包含way
GET test_search_index/_search?q=username:(alfred  NOT way)
# username包含alfred且必须包含way,+号必须写经过encode后的值
GET test_search_index/_search?q=username:(alfred  AND %2Bway)
5.数值和日期范围查询

-区间写法,闭区间用[],开区间用{}

age:[1 TO 10] 意为 1<=age<=10
age:[1 TO 10} 意为 1<=age<10
age:[1 TO] 意为 age >=1
age:[* TO 10] 意为 <=10

-算数写法

age:>=1
age:(>=1 AND <= 10)
age:(+>=10 +<=10)

# 检索1<=age<=23
GET test_search_index/_search?q=age:[1 TO 23]

# 检索age<=20
GET test_search_index/_search?q=age:[* TO 20]

# 检索1<=age<28
GET test_search_index/_search?q=age:([1 TO 28})

# 检索 20<=age<=28
GET test_search_index/_search?q=age:(>=20 AND <=28)
GET test_search_index/_search?q=age:(%2b>=20 %2B<=28)
6.通配符查询

name:t?m
name:tom*
name:t*m

通配符匹配执行效率低,且占用较多内存,不建议使用
如无特殊需求,不要将?/*放在最前面

GET test_search_index/_search?q=username:/[a]?.l*/
{
  "profile":true
}
7.正则和占位匹配

-正则匹配

name:/[mb]oat/

-模糊匹配

name:roam~1
匹配与 roam 差 1 个 character 的词,如 foam roams 等

-近似度匹配(proximity search)

"fox quick"~5
以 term 为单位进行差异比较,比如"quick for" "quick brown fox"都会匹配

GET test_search_index/_search?q=username:alfed~

GET test_search_index/_search?q=job:"java engineer"~2
  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    106 引用 • 99 回帖 • 426 关注

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...