Elasticsearch篇之Search API介绍
程序员文章站
2024-01-19 17:16:28
...
Elasticsearch篇之Search API介绍
1 SearchAPI概览
实现对es中存储的数据进行查询分析,endpoint为_search,如下所示:
查询主要有两种形式
2 URISearch详解与演示
通过url query参数来实现搜索,常用参数如下:
- q指定查询的语句,语法为Query String Syntax
- df q中不指定字段时默认查询的字段,如果不指定,es会查询所有字段
- sort 排序
- timeout 指定超时时间,默认不超时
- from、size 用于分页
- term 与 phrase
- alfred way 等效于 alfred or way
- “alfred way”词语查询,要求先后排序
- 泛查询
- alfred 等效于在所有字段中去匹配该term
- 指定字段
- name:aflred
- Group分组设定,使用括号指定匹配的规则
- (quick OR brown)AND fox
- status:(active OR pending) title:(full text search)
创建索引,生成测试文档
PUT my_index_search
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
}
}
POST my_index_search/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 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-02","isMarried":false}
# 查询所有字段中有alfred的文档
GET my_index_search/_search?q=alfred
# 设置profile可以看具体的查询语句
GET my_index_search/_search?q=alfred
{
"profile": true
}
GET my_index_search/_search?q=username:alfred
GET my_index_search/_search?q=username:alfred
{
"profile": true
}
# username:alfred和way是OR的关系
GET my_index_search/_search?q=username:alfred way
{
"profile": true
}
# PhraseQuery词语的查询
GET my_index_search/_search?q=username:"alfred way"
{
"profile": true
}
# "description": "username:alfred username:way" 下面描述
GET my_index_search/_search?q=username:(alfred way)
{
"profile": true
}
- 布尔操作符
- AND(&&) OR(||) NOT(!)
- name:(tom NOT lee)
- 注意大写,不能小写
- + - 分别对应must和must_not
- name:(tom +lee -alfred) 或者 name:((lee && !alfred)||(tome && lee && !alfred))
- + 在url中会被解析为空格,要使用encode后才可以,为%2B
GET my_index_search/_search?q=username:alfred AND way
{
"profile": true
}
GET my_index_search/_search?q=username:(alfred AND way)
{
"profile": true
}
GET my_index_search/_search?q=username:(alfred NOT way)
{
"profile": true
}
GET my_index_search/_search?q=username:(alfred +way)
{
"profile": true
}
GET my_index_search/_search?q=username:(alfred %2Bway)
{
"profile": true
}
- 范围查询,支持数值和日期
- 区间写法,闭区间用[],开区间{}
- age:[1 TO 10]意为 1<=age<=10
- age:[1 TO 10}意为 1<=age<10
- age:[1 TO]意为 age>=1
- age:{* TO 10]意为age<=10
- 算术符号写法
- age:>=1
- age:(>=1 && <=10)或者 age:(+>=1 +<=10)
- 区间写法,闭区间用[],开区间{}
GET my_index_search/_search?q=username:alfred age:>20
GET my_index_search/_search?q=username:alfred AND age:>20
GET my_index_search/_search?q=birth:(>1980 AND <1990)
- 通配符查询
- ? 代表1个字符, * 代表0或多个字符
- name:t?m
- name:tom*
- name:t*m
- 通配符匹配执行效率低,且占用较多内存,不建议使用
- 如无特殊需求,不要将?/ * 放在最前面
GET my_index_search/_search?q=username:alf*
- 正则表达式匹配
GET my_index_search/_search?q=username:/[a]?l.*/
- 模糊匹配fuzzy query
- name:roam~1
- 匹配roam差1个character的词,比如foam、roams等
- 近似度查询proximity search
- “fox quick”~5
- 以term为单位进行差异比较,比如“quick fox” “quick brown fox”都会被匹配
GET my_index_search/_search?q=username:alfed~1
GET my_index_search/_search?q=job:"java engineer"~2
3 QueryDSL简介
4 字段类查询简介及match-query
5 相关性算分
6 match-phrase-query_音频
7 query-string-query
8 simple-query-string-query
9 term-terms-query
10 range-query
11 复合查询介绍及ConstantScore
12 bool-query
13 count-and-source-filtering
推荐阅读
-
Elasticsearch篇之Search API介绍
-
使用 Laravel Generator 快速为 Laravel 应用生成CRUD、API、测试用例代码 -- 介绍篇
-
详解服务端预渲染之Nuxt(介绍篇)
-
ES 19 - Elasticsearch的检索语法(_search API的使用)
-
ElasticSearch之termvector介绍
-
Vue学习笔记进阶篇之vue-cli安装及介绍
-
详解服务端预渲染之Nuxt(介绍篇)
-
.NET Core实战项目之CMS 第十三章 开发篇-在MVC项目结构介绍及应用第三方UI
-
elasticsearch入门之核心概念篇
-
Elasticsearch Java API 的使用(14)—优化索引创建之setting设置、写入优化