elastatic search 查询语法

es中的数据可以分为两种:一种是准确值;一种是全文检索值。


全文搜索使用分词器。es提供多种分词器。

分词器的作用简单描述:

比如文档q有一个字段date 值为2016-03-28.

如果是准确值,将会存储为2016-03-28;如果使用分词器,将存储为,2016,03,28 这样如果查询GET /_search?q=2016,所有文档将匹配。查询GET /_search?q=2016-03-28,所有文档将匹配。查询GET /_search?q=date:2014-09-15,只有准确值可以匹配。查询GET /_search?q=date:2014,所有值都不能匹配。


mapping:用来匹配数据类型。

 

ES支持的基本数据类型 

String:string

Whole number:byte,short,integer,long

Floating-point:float,double

Boolean:boolean

Date:date

复杂数据类型

null:表示方式如下几种

"null_value":null"empty_array":[]"array_with_null_value":[ null ]

array (数组中的所有值必须为相同的数据类型,ES将根据数组中第一个元素的值来确定数组中数据的类型,array是无序的)

object:


{"gb": { "tweet": { "properties": { "tweet": "user": { "type": "properties": { "id":{ "type": "string" }, "object",{ "type": "string" },{ "type": "string" },{ "type": "long" },"gender":"age":"name": {"type":"properties": {"object", "full": { "type": "string" }, "first": { "type": "string" }, "last": { "type": "string" }} }} }} }

} }


查询使用全路径,结果可能如下

"user.name.first":[john],

"user.name.last" [alex,jones, lisa, smith,mary, white]


mapping index属性:

analyzed:分词(默认)


not_analyzed:不分词
no:不能被索引


eg:

{
"tag": {
            "type":     "string",
            "index"   “not_analyzed"

}
}

除String类型外,只有not_analyzed和no两种选择。



mapping analyzer属性:指定分词器类型
{

"tweet": {

            "type":     "string",
            "analyzer": "english"
        }
}
已经存在的mapping 只能新增字段而不能修改字段。

query DSL:


查询所有
  GET /_search
    {

  "query": {

   
     

        "match_all": {}

}
}



基本格式:


    {
        QUERY_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,…
        }


}


    {
        QUERY_NAME: {
            FIELD_NAME: {
                ARGUMENT: VALUE,
                ARGUMENT: VALUE,…
            }


} }


合并多个查询 


{
"bool": {
        "must": { "match":
        "should": [

h":

            { "boo
             { "mat

cl": {

}} ],

{ "email": "business opportunity" }},

{ "starred":
            

  true }},

"inbox" }}, "must_not": { "spam": true }}

"must":

 
{ "folder"

:          "minimum_should_match": 1

        }


}


DSL分为query dsl和filter dsl

query DSL 问的是how well/filter DSL 问的是yes|no。即使准确值时用query也可以,但还是推荐使用filter,因为filter有缓存。而且每个文档只占1bit。

性能上看 query比filter更重一些。filter只是获取yes的文档,而filter还需要计算相关度。

在需要全文搜索或相关度时使用query。其他所有情况下使用filter。

term Filter:

适用于匹配准确值:numbers, dates, Booleans, or not_analyzed exact-value string fields。

{ "term":{"age":26}}{"term":{"date":"2014-09-01"}}{"term":{"public":true}}{"term":{"tag":"full_text"}}

terms Filter与term filter类似。不同的是可以匹配多个条件。
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

range Filter:适用于numbers or dates匹配大小范围:
{


        "range": {
            "age": {
"gte":20,
"lt":30}
}}
gt:Greater thangte:Greater than or equal tolt:Less thanlte:Less than or equal to

exists and missing Filters:用来判断一个字段有一个或多个值存在还是没有任何值存在。is——null的判断。
{"exists": { "field": "title" }}

bool Filter:


mustThese clauses must match, like and.must_notThese clauses must not match, like not.shouldAt least one of these clauses must match, like or.



match_all Query:查询所有

multi_match Query:允许同一查询在多个字段执行:

{"multi_match": { "query":"full text search", "fields":[ "title", "body" ] }
}

使用filtered可以合并query和filter。t

{ "filtered": { "query": {
"match": { "email": "business opportunity" }}, "filter": { "term": { "folder": "inbox" }}} 
}



Validating Queries:用来检查查询语句是否合法
GET /gb/tweet/_validate/query { "query": { "tweet" : { "match" : "really powerful" }} }
结果可以根据valid字段判断是否合法
{"valid" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0} }

Understanding Errors:
使用explain可以查看更详细的错误信息

结果排序默认按照相关度。
查询url中的explain字段可以显示score的计算方式。

分页:
GET /_search?size=5&from=10 (慎用,耗内存)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值