如何在 ElasticSearch 中组合多个查询 [英] How do I combine multiple queries in ElasticSearch

查看:34
本文介绍了如何在 ElasticSearch 中组合多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码供参考.我正在尝试制作一个 hubot 插件,该插件记录到 elasticsearch,然后使用 hubot 命令搜索这些日志.

For reference here is the code. I am trying to make a hubot plugin that logs to elasticsearch and then uses hubot commands to search those logs.

https://gist.github.com/4050748

我正在尝试检索匹配两个查询的记录.

I am trying to retrieve records that match two queries.

{ 
  query: { 
        match: {
          user: "SomeUsername" 
        }, 
        range: {
          date: {
            from: (Date.now() - 3600) 
          }
        }
  },
  size: 50 
}

我期待:

  • 最多 50 条记录
  • 具有给定用户的记录
  • 最近一小时的记录

我明白了:

  • 最多 10 条记录
  • 具有给定用户的记录
  • 从任何时候开始

如何获取过去一小时内使用某个用户名的所有记录?我需要将 match_all 与过滤器一起使用吗?我的尝试不受支持吗?

How do I get all the records with some username in the last hour? Do I need to use match_all with filters? Is what I am attempting unsupported?

在 SQL 中会是这样的:

In SQL it would be something like:

Select (*) from messages where user_name = ? and time > ?

推荐答案

你需要使用bool query 将不同的查询组合在一起.然后,您可以选择每个查询是必须匹配、应该匹配(可选)还是不能匹配.

You need to use the bool query to combine different queries together. You can then choose whether each single query must match, should match (optional), or must not match.

这篇关于如何在 ElasticSearch 中组合多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆