简体   繁体   English

在搜索结果中排除索引数据-ElasticSearch(轮胎)

[英]Exclude indexed data in search results - elasticsearch (tire)

I'm trying to use elasticsearch via tire gem for a multi-tenant app. 我正在尝试通过轮胎宝石将Elasticsearch用于多租户应用程序。 The app has many accounts with each account having many users. 该应用程序有许多帐户,每个帐户都有许多用户。

Now I would like to index the User based on account id. 现在,我想根据帐户ID为用户编制索引。

User Model: 用户模型:

  include Tire::Model::Search
  mapping do
    indexes :name, :boost => 10
    indexes :account_id
    indexes :company_name
    indexes :description
  end

  def to_indexed_json
    to_json( :only => [:name, :account_id, :description, :company_name], 
           )
  end

Search Query: 搜索查询:

  User.tire.search do
    query do
      filtered do
        query { string 'vamsikrishna' }
        filter :term, :account_id => 1
      end
    end
  end

The filter works fine and the results are displayed only for the filtered account id (1). 过滤器工作正常,并且仅显示过滤后的帐户ID(1)的结果。 But, when I search for a query string 1: 但是,当我搜索查询字符串1时:

  User.tire.search do
    query do
      filtered do
        query { string '1' }
        filter :term, :account_id => 1
      end
    end
  end

Lets say there is an user named 1. In that case, the results are getting displayed for all the users with account id 1 and the user too. 假设有一个名为1的用户。在这种情况下,将为帐户ID为1的所有用户以及该用户显示结果。 This is because I added account_id in the to_indexed_json. 这是因为我在to_indexed_json中添加了account_id。 Now, how can I display only the user with name 1? 现在,如何只显示名称为1的用户? (All users should not be available in the hits. Only the user with name 1 should be displayed) (所有用户都不能在匹配中使用。仅显示名称为1的用户)

When there are no users with 1 as name or company name or description, I just don't want any results to be displayed. 当没有用户使用名称或公司名称或描述作为1时,我只是不希望显示任何结果。 But, in my case as I explained I would get all the users in the account id 1. 但是,按照我的解释,我将使所有用户都进入帐户ID 1。

You are searching on the _all special field, which contains a copy of the content of all the fields that you indexed. 您正在搜索_all特殊字段,该字段包含您所索引的所有字段的内容的副本。 You should search on a specific field to have the right results like this: field_name:1 . 您应该在一个特定的字段上进行搜索,以得到正确的结果,例如: field_name:1

If you want you can search on multiple fields using the query string : 如果需要,可以使用查询字符串在多个字段上进行搜索

{
    "query_string" : {
        "fields" : ["field1", "field2", "field3"],
        "query" : "1"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM