在Elasticsearch使用URI花式搜索

通过URI实现搜索

URI Search 是通过在URI上传入参数定义你的查询条件,不支持所有的DSL但是更加便利,常用传入参数如下:

curl -XGET -H "Content-Type:application/json" "http://localhost:9200/<index>/_search?q=<parameter>" 
  • default_operator
    设置字符串查询时的操作符,共有三类ORANDNOT,在查询时术语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
    truefalse是否开启分数计算解释,为查询出来的每一个文档,解释分数计算的过程(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 or false,则表示是否返回文档中_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_fetchdfs_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"
# 最终只命中一条文档

指定字段和泛查询

  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)查询

  1. Term术语精准查询
  2. 语句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,语句查询要保证查询条件的顺序

范围查询和算数符号

  1. 范围查询
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" : {...}
}
  1. 算数符号进行范围查询
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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

喜马拉雅以南

奶茶,干杯?!

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

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

打赏作者

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

抵扣说明:

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

余额充值