rails:使用数据地址键入搜索使用elasticsearch chewy gem

时间:2016-03-26 06:00:48

标签: ruby-on-rails ruby elasticsearch rubygems ruby-on-rails-4.2

我使用耐嚼宝石弹性研究。

我有LocationsIndex,映射:

class LocationsIndex < Chewy::Index
  settings analysis: {
    analyzer: {
      folding: {
          tokenizer: "standard",
          filter:  [ "lowercase", "asciifolding" ]
        },
      sorted: {
        tokenizer: 'keyword',
        filter: ['lowercase', 'asciifolding']
      }
    }
  }

  define_type Location do
    field :name, type: 'string', analyzer: 'standard' do
      field :folded, type: 'string', analyzer:   "folding"
    end
    field :address, type: 'string', analyzer: 'standard' do
      field :address, type: 'string', analyzer: 'folding'
    end
    field :locations, type: 'geo_point', value: ->{ {lat: lat, lon: lon} }
  end

end

当我查询时:

LocationsIndex::Location.query(
        multi_match: {
          query: keyword,
          fields: ["address", "address.folded" ,"name", "name.folded"]
        }
      )

数据样本:

  

{“take”:2,“timed_out”:false,“_ shards”:{       “总数”:5,       “成功”:5,       “失败”:0},“点击”:{       “总数”:10,       “max_score”:1.0,       “命中”:[{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“131”,         “_score”:1.0,         “_source”:{“name”:“ViệtNam”,“address”:“ViệtNam”,“locations”:{“lat”:16.9054,“lon”:106.576}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“136”,         “_score”:1.0,         “_source”:{“name”:“Quan truong Ngo Mon”,“address”:“23/8,ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4669,“lon”:107.58} }       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“132”,         “_score”:1.0,         “_source”:{“name”:“ThừaThiênHuế”,“address”:“ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4674,“lon”:107.591}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“137”,         “_score”:1.0,         “_source”:{“name”:“Phu Van Lau”,“address”:“23/8,ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4655,“lon”:107.581}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“133”,         “_score”:1.0,         “_source”:{“name”:“Ha Noi”,“address”:“Ha Noi,ViệtNam”,“locations”:{“lat”:16.4674,“lon”:107.591}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“138”,         “_score”:1.0,         “_source”:{“name”:“Cau gia Vien”,“address”:“Le Duan,ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4601,“lon”:107.571}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“134”,         “_score”:1.0,         “_source”:{“name”:“TP胡志明”,“地址”:“TP胡志明,ViệtNam”,“位置”:{“lat”:16.4674,“lon”:107.591}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“139”,         “_score”:1.0,         “_source”:{“name”:“Chua thien Pagoda”,“address”:“Kim Long,ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4537,“lon”:107.545}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“130”,         “_score”:1.0,         “_source”:{“name”:“ViệtNam”,“address”:“ViệtNam”,“locations”:{“lat”:16.9054,“lon”:106.576}}       },{         “_index”:“位置”,         “_type”:“位置”,         “_id”:“135”,         “_score”:1.0,         “_source”:{“name”:“Dai Noi Hue”,“address”:“23/8,ThừaThiênHuế,ViệtNam”,“locations”:{“lat”:16.4698,“lon”:107.577}}       }]}}

当我使用keyword =“viet nam”运行查询时

result :
 _id = [131,132,,133,134,135,136,137,138,139,130]  # => OK working

但我在使用keywork =“thua thien hue”运行查询时

result :
     _id = [132,135,139]  # => Don't working ???, should have been: _id = [132,135,136,137,138,139]

与keywork =“hue”相同

result :
         _id = [132,135]  # => Don't working ???, should have been: _id = [132,135,136,137,138,139]

包含上述单词的搜索结果(添加类型或执行任何操作)

1 个答案:

答案 0 :(得分:0)

声明字段address时出错。您宣布address.address而不是address.folded。换句话说,声明应该是:

field :address, type: 'string', analyzer: 'standard' do
  field :folded, type: 'string', analyzer: 'folding'
end