Elasticsearch 5.4 Query DSL

Elasticsearch提供了丰富的查询语句DSL,查询可分2类:

  1. Leaf Query:查询特定字段特定值的查询,可以单独使用,比如match查询、term查询、range查询。
  2. Compound Query:组合查询,组合多个简单查询或者特殊查询。

先占坑,有时间更新Y(^_^)Y

一、Query和Filter的区别

Query是真正的信息检索,会用到检索模型进行评分,回答的是查询和文档的匹配程度;Filter是过滤机制,回答的是Yes Or NO。

#二、Match All Query
match all query会返回所有文档,文档的得分都是1。

GET /_search
{
    "query": {
        "match_all": {}
    }
}

也可以通过boost参数修改得分:

GET /_search
{
    "query": {
        "match_all": { "boost" : 1.2 }
    }
}

match_none是match_all的取反,不返回任何文档:

GET /_search
{
    "query": {
        "match_none": {}
    }
}

三、Full Text Query(全文查询)

3.1 match query

GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}

message是被搜索字段,this is a test是query内容,分词后query中的任何一个关键字被匹配文档就会被搜索到。如果想查询匹配所有关键词的文档,可以用and操作符连接:

GET /_search
{
    "query": {
        "match" : {
            "message" : {
                "query" : "this is a test",
                "operator" : "and"
            }
        }
    }
}

3.2 match phrase query

match phrase query首先会把query内容分词,分词器可以自定义,同时文档还要满足以下两个条件才会被搜索到:

  1. 分词后的所有词项都要出现在该字段中
  2. 字段中的词项顺序要一致

例如,有三个文档:

{ "foo":"I just said hello world" }

{ "foo":"Hello world" }

{ "foo":"World Hello" }

使用match_phrase查询"hello world":

{
  "query": {
    "match_phrase": {
      "foo": "Hello World"
    }
  }
}

只有前2个文档会被匹配。

3.3 match phrase prefix query

match_phrase_prefix和match_phrase类似,只不过match_phrase_prefix支持最后一个term前缀匹配:

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
        }
    }
}

3.4 multi match query

multi_match是match的升级,用于搜索多个字段:

GET books/_search
{
  "query": {
    "multi_match" : {
      "query":    "java虚拟机", 
      "fields": [ "title", "description" ],
      "operator" : "and"
    }
  }
}

字段的名称可以使用通配符:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":    "Will Smith",
      "fields": [ "title", "*_name" ] 
    }
  }
}

同时,也可以用指数符指定权重:

GET /_search
{
  "query": {
    "multi_match" : {
      "query" : "this is a test",
      "fields" : [ "subject^3", "message" ] 
    }
  }
}

##3.5 common terms query

##3.6 query string query

##3.7 simple query string query

#四、Term Level Query(term级别查询)

##4.1 term query
##4.2 terms query
##4.3 range query
##4.4 exists query
##4.5 prefix query
##4.6 wildcard query
##4.7 regexp query
##4.8 fuzzy query
##4.9 type query
##4.10 ids query
#五、Compound Query(组合查询)
##5.1 constant score query
##5.2 bool query
##5.3 Dis max query
##5.4 Function Score query
##5.5 boosting query
##5.6 indices query
#六、Joining Query(嵌套查询)
##6.1 nested query
##6.2 has child query
##6.3 has parent query
##6.4 parent id query
#七、Geo Query(地理位置查询)
##geoshape query
##geo bounding query
##geo distance query
##geo distance range query
##geo polygon query
#八、Specialized Query(特殊查询)
##8.1 more like this query
##8.2 template query
##8.3 script query
##8.4 percolate query
#九、Span Query
##9.1 span term query
##9.2 span multi term query
##9.3 span first query
##9.4 span near query
##9.5 span or query
##9.6 span not query
##9.7 span containing query
##9.8 span within query
##9.9 span field masking query

十、minimum should match

#十一、multi term query rewrite

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

esc_ai

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值