文章目录
通过URI实现搜索
URI Search 是通过在URI上传入参数
定义你的查询条件
,不支持所有的DSL
但是更加便利,常用传入参数如下:
curl -XGET -H "Content-Type:application/json" "http://localhost:9200/<index>/_search?q=<parameter>"
default_operator
设置字符串查询时的操作符,共有三类OR
、AND
、NOT
,在查询时术语term
将可能进行分组
curl -XGET -H "Content-Type:application/json" "http://localhost:9200/<index>/_search?/_search?q=customer_full_name:(Eddie Underwood)&default_operator=AND" -d '{"profile":"true"}'
q
这个参数需要填写查询语句,Lucene
中的字符串查询语法Query string syntax
,在使用时会有指定段查询、泛查询、term分组、语句phrase
查询、范围查询、正则表达式、通配符查询(效率低占内存大,通配符在查询语句前方则存在更大的问题)、模糊匹配与近似查询这类概念。
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley"
df
在q
参数的查询语句中没有指定field则使用df
参数的值指定q
中值所属的参数。
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=Riley&df=customer_last_name"
# 查询customer_last_name为Riley的文档数据
explain
true
或false
是否开启分数计算解释,为查询出来的每一个文档,解释分数计算的过程(IDF计算跟词频相关),默认为false
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&explain=true"
from
文档开始的偏移量,与size
合用进行分页,但是也存在深度分页的性能问题
,默认为0
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&from=10"
size
设置命中(his
)的文档返回多少条,默认为10
条
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&from=10&size=1"
_source
可以设置true
orfalse
,则表示是否返回文档中_source
这个元数据,也可以是字段集合,则表示需要返回原文哪些字段(field
)
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source=false"
# 不返回_source字段即原文
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source=currency,customer_full_name"
# 只返回currency和customer_full_name两个字段
_source_excludes
传入字段名集合,返回文档中需要排除的字段
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source_excludes=manufacturer,products"
# 将返回文档中的manufacturer和products两个字段排除掉
_source_includes
传入字段名集合,返回文档中只需要的字段
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley&_source_includes=customer_full_name"
# 只返回文档中的customer_full_name字段
analyzer
设置q
参数中的查询语句使用的分词器
(中文分词器需要提前安装并重启集群),集群默认使用standard
分词器
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=Mary Bailey&df=customer_last_name&analyzer=simple"
# 此处使用的simple分词器,但是应为Token Filters会将单词转成小写,故有可能查不到
analyze_wildcard
默认情况下,不分析查询字符串中的通配符术语。 将此值设置为true,将尽力分析这些值,默认为false
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=725?4*&df=order_id&analyze_wildcard=false"
-
sort
-
stored_fields
返回文档的选择性字段,该字段值存储于原文_source
之外,在指定mappings
是设置在每个字段
# 设置字段是否在store存储
PUT test_demo
{
"mappings": {
"properties": {
"full_name":{
"type": "keyword",
"store": true
}
}
}
}
# 调用API查询full_name字段,命中则只返回full_name字段
curl -XGET 'http://localhost:9200/test_demo/_search?q=customer_last_name:"Mary Bailey"&stored_fields=full_name'
timeout
设置超时时间search_type
搜索类型,目前只有query_then_fetch
和dfs_query_then_fetch
两个可用选项,默认是query_then_fetch
terminate_after
为每个分片最大的检索文档的值,到达后将提前终止
curl -XGET "http://localhost:9200/kibana_sample_data_ecommerce/_search?q=elys*&df=user&_source=user&terminate_after=1"
# 最终只命中一条文档
指定字段和泛查询
- 指定字段
可以在q
参数中指定,或者使用df
参数指定查询字段
GET kibana_sample_data_ecommerce/_search?q=customer_last_name:Riley
{
"profile": "true" //这个选项打开主要是为能够查看到查询的过程,下列同样如此
}
//响应如下,只贴出查询过程部分
{
"type" : "TermQuery", //@1
"description" : "customer_last_name:riley", //@2
"time_in_nanos" : 299386,
"breakdown" : { ... }
}
@1 精确查询term
精确查询
@2 精确查询customer_last_name值为riley的文档
2. 泛查询
GET kibana_sample_data_ecommerce/_search?q=Riley
{
"profile": "true" //这个选项打开主要是为能够查看到查询的过程,下列同样如此
}
//响应如下,只贴出查询过程部分
{
"type" : "DisjunctionMaxQuery", //@1
"description" : "(products.manufacturer:riley | MatchNoDocsQuery("failed [products.base_unit_price] query, caused by number_format_exception:[For input string: "Riley"]") ", //@2
"time_in_nanos" : 299386,
"breakdown" : { ... }
}
@1 使用DisjunctionMaxQuery
生成多个子查询的合集查询,是最高的评分作为最终得分
@2 精确查询customer_last_name值为riley的文档
Term与语句(phrase)查询
- Term术语精准查询
- 语句
phrase
查询
GET kibana_sample_data_ecommerce/_search?q="Kamal Richards"&df=customer_full_name
{
"profile": "true"
}
//响应如下,只贴出查询过程部分
{
"type" : "PhraseQuery", //@1
"description" : """customer_full_name:"kamal richards"""", //@2
"time_in_nanos" : 308390,
"breakdown" : { ... }
}
@1 使用的PhraseQuery
语句查询
@2 指定查询customer_full_name
的值为kamal richards
,语句查询要保证查询条件的顺序
范围查询和算数符号
- 范围查询
GET kibana_sample_data_ecommerce/_search?q=taxful_total_price:[30.00 TO 43.0]
{
"profile": "true"
}
//响应如下,只贴出查询过程部分
{
"type" : "IndexOrDocValuesQuery", //@1
"description" : "taxful_total_price:[30.0 TO 43.0]", //@2
"time_in_nanos" : 286535,
"breakdown" : {...}
}
- 算数符号进行范围查询
GET kibana_sample_data_ecommerce/_search?q=order_date:(>=2020-03-27 AND <2020-03-28)
{
"profile": "true"
}
//
{
"type" : "BooleanQuery", //@1
"description" : "+order_date:[1585267200000 TO 9223372036854775807] +order_date:[-9223372036854775808 TO 1585353599999]", //@2
"time_in_nanos" : 616947,
"breakdown" : : {...},
"children" : [ //@3
{
"type" : "IndexOrDocValuesQuery", //@4
"description" : "order_date:[1585267200000 TO 9223372036854775807]", //@5
"time_in_nanos" : 227389,
"breakdown" : {...}
},
{
"type" : "IndexOrDocValuesQuery",
"description" : "order_date:[-9223372036854775808 TO 1585353599999]",
"time_in_nanos" : 137528,
"breakdown" : {...}
}
]
}
@1 使用BooleanQuery
对查询进行分组
@2 对查询的描述,必须
order_date满足[1585267200000 TO 9223372036854775807]
和[-9223372036854775808 TO 1585353599999]
两个条件同时满足
@3 本次查询包含的子查询,即BooleanQuery
将条件分成了两个IndexOrDocValuesQuery
查询
@4
@5
通配符查询
正则表达式
模糊匹配与近似查询
Request Body Search查询
使用SearchTemplate查询
允许你使用mustache language
对现有模板进行填充
GET shakespeare/_search/template
POST kibana_sample_data_ecommerce/_search
{
"profile": true,
"query": {
"match_all": {}
}
}
GET kibana_sample_data_ecommerce/_search
{
"profile": true,
"query": {
"match_phrase": {
"FIELD": "PHRASE"
}
}
}
GET kibana_sample_data_ecommerce/_search?q=customer_full_name:(Eddie Underwood)&default_operator=AND
{
"profile":"true"
}
GET kibana_sample_data_ecommerce/_search/template?pretty
{
"source": {
"query": {
"match_phrase": {
"{{my_field}}": "{{my_value}}"
}
},
"size": "{{my_size}}"
},
"params": {
"my_routing":"pVeQmXABN95HgAacawWq",
"my_field": "customer_full_name",
"my_value": "Eddie Underwood",
"my_size": 5
}
}
POST /_scripts/my_search_template?pretty
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match_phrase": {
"{{my_field}}": "{{my_value}}"
}
}
}
}
}
GET _scripts/my_search_template
GET kibana_sample_data_ecommerce/_search/template
{
"id": "my_search_template",
"params": {
"my_field": "customer_full_name",
"my_value": "Eddie Underwood"
}
}
GET _render/template
{
"source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}
//验证一个搜索模板
GET _render/template
{
"id": "my_search_template",
"params": {
"my_field": "customer_full_name",
"my_value": "Eddie Underwood"
}
}
GET /_search/template?pretty
{
"id": "my_search_template",
"params": {
"my_field": "customer_full_name",
"my_value": "Eddie Underwood"
},
"explain": true
}