ruby - 使用 ruby​​ elasticsearch gem,如何使用多个参数进行查询

标签 ruby elasticsearch

我基本上想做一个搜索:

select all from index_name where tag = 'big data' and city = 'tokyo'

代码是:

require "elasticsearch"
client = Elasticsearch::Client.new log: true
client.search index: 'candidates', body:
{
  query: {

    match: {
          tags: search_term
           }
        }
}

效果很好。但是,当我更改为:

match: {
          tags: search_term, city: 'Tokyo'
           }

也就是说,只要添加另一个参数,我就会收到错误。

我尝试添加

filter: {
            city: 'Tokyo'
       }

也没有成功。

谢谢大家,最好的方法是什么?

最佳答案

您需要检查Elasticsearch query DSL 。对于简单的“A 和 B”查询,您只需使用 bool / must query 。用这个替换你的 body :

client.search index: 'candidates', body:
{
  query: {
    bool: {
      must: [
        {
          match: {
            tags: 'big data'
          }
        },
        {
          match: {
            city: 'tokyo'
          }
        }
      ]
    }
  }
}

关于ruby - 使用 ruby​​ elasticsearch gem,如何使用多个参数进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678135/

相关文章:

ruby-on-rails - 未定义的局部变量或方法 "articles_path"

ruby-on-rails - 在 rails 中将一个表记录复制到另一个表记录

ruby - 如何在成功的 oauth 授权后使用 tumblr api 获取 base-hostename(博客名称)

elasticsearch - docker 容器的默认 elasticsearch 配置

node.js - 使用单个 firehose 将数据索引到多个索引

search - ElasticSearch过滤地理距离

ruby-on-rails - 如何在 Controller Action 中切换 bool 属性

ruby - 如果在 git pull/merge 之后更新了 gemfile,如何自动运行 bundle install?

elasticsearch - 无痛(Elasticsearch)将任何类型的值转换为整数

caching - Elasticsearch 缓存清除似乎没有达到我的预期