1

这似乎是一个非常简单的问题,但我从其他解决方案和网站尝试过的一切都不起作用。我有三个我不想索引或查询的字段-- 、和--但是即使我没有指定它们应该被索引或查询,elasticsearch 仍然从这些字段返回值:p_s。大约一半,这篇文章说要说索引,但他们没有指出这会发生在哪里。:gender:part_of_speechno

任期控制器:

  def search
    @terms = Term.search(params[:query]).page(params[:page])
  end

模型:

require 'elasticsearch/model'

class Term < ActiveRecord::Base

include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1, number_of_replicas: 0 },
    do

    mappings dynamic: 'false' do
      indexes :id, index: :not_analyzed
      indexes :name, analyzer: :spanish_analyzer
      indexes :definition, analyzer: :combined_analyzer
      indexes :etymology1, analyzer: :combined_analyzer
      indexes :etymology2, analyzer: :combined_analyzer
      indexes :uses, analyzer: :combined_analyzer
      indexes :notes1, analyzer: :combined_analyzer
      indexes :notes2, analyzer: :combined_analyzer
    end
  end

  def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'],
            operator: 'and'
          }
        }
      }
    )
  end
end

# Delete the previous term index in Elasticsearch
Term.__elasticsearch__.client.indices.delete index: Term.index_name rescue nil

# Create the new index with the new mapping
Term.__elasticsearch__.client.indices.create \
  index: Term.index_name,
  body: { settings: Term.settings.to_hash, mappings: Term.mappings.to_hash }

# Index all term records from the DB to Elasticsearch
Term.import(force: true)
4

2 回答 2

1

要将字段标记为非索引,请使用以下命令:

mappings dynamic: 'false' do
    ...
    indexes :p_s, index: :no
    indexes :gender, index: :no
    indexes :part_of_speech, index: :no
    ...
end

默认情况下,elasticsearch 返回"_source"key 下的所有文档字段。要仅获取特定字段,您可以像这样在顶级查询级别指定fields数组

def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'],
            operator: 'and'
          }
        },
        fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'notes1', 'notes2']
      }
    )
  end

或过滤"_source"

def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'],
            operator: 'and'
          }
        },
        '_source': ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'notes1', 'notes2']
      }
    )
end

有关更多信息,请参阅 Elasticsearch 源过滤文档。

使用multi_match子句时,内部fields元素指定要在其上运行搜索的字段,并且可以选择像您的示例中那样进行提升。外部fields或“_source”子句依次确定要返回的字段,这就是您要返回的字段。

要在调试 elasticsearch 查询时更好地了解正在发生的事情,请使用Sense之类的工具。当你得到你想要的结果时,将查询转移到 ruby​​ 代码可能比反之更容易。

于 2016-06-24T19:10:12.397 回答
0

我认为使用包含的elasticsearch方法很有意义。但是,在我自己的情况下,在我的模型中,我做了类似的事情,针对您自己的情况进行了修改:

def as_indexed_json
  as_json(only: [:id, :name, :definition, :etymology1, :etymology2, :uses, :notes1, :notes2])
end

这应该可以工作,因为默认情况下 Elasticsearch 会调用as_indexed_json模型中的方法来获取它需要索引的数据。

于 2016-06-24T18:58:47.177 回答