简体   繁体   English

使用ElasticSearch在具有不同区域设置的字段上排序

[英]Sort on field with different locale with ElasticSearch

In project, there was a need in sorting records by one field stored in different locales. 在项目中,需要按存储在不同语言环境中的一个字段对记录进行排序。 What I have: table "companies", there is no field named "name", but there is a table: 我所拥有的:表“ companies”,没有名为“ name”的字段,但是有一个表:

create_table "company_localizations" do |t|
  t.integer "company_id"
  t.string "locale"
  t.string "name"
  ...
end

I have index company.rb: 我有索引company.rb:

mapping do
  indexes :name, :type => "multi_field",
    :fields => {
       :name => {:type => "string", :index => "analyzed"},
       :name_sort => {:type => "string", :index => "not_analyzed"}
    }
end

I need to make sort on this field. 我需要在该字段上进行排序。 I'm using ElasticSearch and Tire gem. 我正在使用ElasticSearch和Tire宝石。

In Tire, use 在轮胎中使用

sort { by "name.name_sort" }

in your case. 在你的情况下。 The name_ prefix in your example is not needed -- without it, you can refer to your field as name.sort . 您的示例中不需要name_前缀-没有它,您可以将字段称为name.sort

See https://github.com/karmi/tire/blob/master/test/integration/sort_test.rb and other integration tests for more info. 有关更多信息,请参见https://github.com/karmi/tire/blob/master/test/integration/sort_test.rb和其他集成测试。

I've got a really strange action of Elastic, but it didn't sort on multi_filed type. 我对Elastic有一个非常奇怪的动作,但是它没有对multi_filed类型进行排序。 There was a problem that in an app it could be russian symbols in english locale and english symbols in russian locale field in the database, so it didn't sort with the multi_filed type... I've successed with changing the multi_filed to nested in such way: 有一个问题,在应用程序中,它可能是数据库中英语语言环境中的俄语符号和俄语语言环境字段中的英语符号,因此它没有使用multi_filed类型进行排序...我已经成功地将multi_filed更改为nested以这种方式:

settings :analysis => {
  :analyzer => {
    :sort_analyzer => {
      "tokenizer" => "keyword",
      "filter" => ["lowercase"]
    }
  }
}

mapping do
  indexes :name do
    indexes :name
    indexes :name_sort, :type => "string", :analyzer => "sort_analyzer"
  end
...
end


def to_indexed_json
 {
    ...
    :name => {
      :name => all_localizations(:name).join(" "),
      :name_sort => all_localizations(:name).join(" ").mb_chars.strip.to_s
    },
    ...
  }
end

That fixes the problem.... 解决了问题。

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

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