【问题标题】:How to make fields on my model not searchable but they should still be available in the _source?如何使我的模型上的字段不可搜索,但它们仍应在 _source 中可用?
【发布时间】:2013-07-27 19:02:00
【问题描述】:

我在 Rails 中将轮胎宝石用于 ElasticSearch。

好的,所以我整天都在与这个作斗争,这就是我已经走了多远。我想让我的模型上的字段不可搜索,但它们仍应在 _source 中可用,以便我可以使用它们对搜索结果进行排序。

我的映射:

mapping do
indexes :created_at, :type => 'date', :index => :not_analyzed
indexes :vote_score, :type => 'integer', :index => :not_analyzed
indexes :title
indexes :description
indexes :tags
indexes :answers do
indexes :description
end
end

我的 to_indexed_json 方法:

def to_indexed_json
{
vote_score: vote_score,
created_at: created_at,
title: title,
description: description,
tags: tags,
answers: answers.map{|answer| answer.description}
}.to_json
end

我的搜索查询:

def self.search(term='', order_by, page: 1)
tire.search(page: page, per_page: PAGE_SIZE, load: true) do
query { term.present? ? string(term) : all }
sort {
by case order_by
when LAST_POSTED then {created_at: 'desc'}
else {vote_score: 'desc', created_at: 'desc'}
end
}
end
end

我现在唯一要解决的问题是如何使 vote_scorecreated_at 字段不可搜索,但在搜索时仍设法使用它们进行排序。

我尝试了indexes :created_at, :type => 'date', :index => :no,但没有成功。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 elasticsearch tire


    【解决方案1】:

    如果我理解你的话,当你将搜索查询发送到 elasticsearch 时,你并没有指定一个字段。这意味着它将在_all 字段中再次执行。这是一个“特殊”字段,它使 elasticsearch 更容易快速使用。默认情况下,所有字段都被索引两次,一次在它们自己的字段中,一次在_all 字段中。 (您甚至可以对这两个索引应用不同的映射/分析器。)

    我认为将字段的映射设置为"include_in_all": "false" 应该适合您(删除"index": "no" 部分)。现在该字段将在其字段名下被标记化(并且您可以使用它进行搜索),但是当在 _all 字段中进行搜索时,它不会影响结果(因为它的任何标记都没有存储在 _all 字段中) .

    Have a read of the es docs on mappings, scroll down to the parameters for each type

    祝你好运!

    【讨论】:

    • 我在这些字段上设置了索引 :created_at, :type => 'date', :include_in_all => false 但即使它们不在字段名下,它们的值仍然可以搜索。这意味着要么我仍然缺少其他东西,要么 include_in_all => false 不是正确的方法。例如,我输入 2013 作为我的查询,我得到了 results ,我相信它来自 created_at 字段。
    • 您必须在更改映射后重新索引。 (我猜你可能已经这样做了,只是确保)
    • 是的,我已重新编制索引,但仍然遇到同样的问题。即使在进一步阅读之后 :include_in_all => false 似乎就是我所需要的,但这些字段仍然可以搜索。还有什么我可能会错过的吗?
    • 我最终采用了只匹配我想要并且有效的字段的方法。这匹配多个字段。 Tire.search(page: page, per_page: PAGE_SIZE, load: true) 查询 { term.present? ? (匹配 [:title, :description, :tags, :answers], term) : all } sort { by case order_by when LAST_POSTED then {created_at: 'desc'} else {vote_score: 'desc', created_at: 'desc'}结束 } 结束
    • 抱歉,我有点不明白为什么您仍然看到热门歌曲。如果您可以从命令行尝试一些 curl 语句,您可能会成功。
    【解决方案2】:

    我最终采用了只匹配我想要的字段的方法,并且有效。这匹配多个字段。

    tire.search(page: page, per_page: PAGE_SIZE, load: true) do
          query { term.present? ? (match [:title, :description, :tags, :answers], term) : all }
          sort {
            by case order_by
                 when LAST_POSTED then {created_at: 'desc'}
                 else {vote_score: 'desc', created_at: 'desc'}
               end
          }
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多