【问题标题】:rails : type search with data address use elasticsearch chewy gemrails :使用数据地址键入搜索使用弹性搜索耐嚼的宝石
【发布时间】:2016-03-26 23:59:13
【问题描述】:

我使用耐嚼的 gem elasticsearch。

我有 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"]
        }
      )

数据样本:

{ "took" : 2, "timed_out" : false, "_shards" : { “总数”:5, “成功”:5, “失败”:0 },“命中”:{ “总数”:10, “最大分数”:1.0, “命中”:[{ "_index" : "位置", "_type" : "位置", “_id”:“131”, “_score”:1.0, "_source":{"name":"Việt Nam","address":"Việt Nam","locations":{"lat":16.9054,"lon":106.576}} }, { "_index" : "位置", "_type" : "位置", “_id”:“136”, “_score”:1.0, "_source":{"name":"Quan truong Ngo Mon","address":"23/8, Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4669,"lon":107.58} } }, { "_index" : "位置", "_type" : "位置", “_id”:“132”, “_score”:1.0, "_source":{"name":"Thừa Thiên Huế","address":"Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4674,"lon":107.591}} }, { "_index" : "位置", "_type" : "位置", “_id”:“137”, “_score”:1.0, "_source":{"name":"Phu Van Lau","address":"23/8, Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4655,"lon":107.581}} }, { "_index" : "位置", "_type" : "位置", “_id”:“133”, “_score”:1.0, "_source":{"name":"Ha Noi","address":"Ha Noi, Việt Nam","locations":{"lat":16.4674,"lon":107.591}} }, { "_index" : "位置", "_type" : "位置", “_id”:“138”, “_score”:1.0, "_source":{"name":"Cau gia Vien","address":"Le Duan, Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4601,"lon":107.571}} }, { "_index" : "位置", "_type" : "位置", “_id”:“134”, “_score”:1.0, "_source":{"name":"TP Ho Chi Minh","address":"TP Ho Chi Minh, Việt Nam","locations":{"lat":16.4674,"lon":107.591}} }, { "_index" : "位置", "_type" : "位置", “_id”:“139”, “_score”:1.0, "_source":{"name":"Chua thien Pagoda","address":"Kim Long, Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4537,"lon":107.545}} }, { "_index" : "位置", "_type" : "位置", “_id”:“130”, “_score”:1.0, "_source":{"name":"Việt Nam","address":"Việt Nam","locations":{"lat":16.9054,"lon":106.576}} }, { "_index" : "位置", "_type" : "位置", “_id”:“135”, “_score”:1.0, "_source":{"name":"Dai Noi Hue","address":"23/8, Thừa Thiên Huế, Việt Nam","locations":{"lat":16.4698,"lon":107.577}} } ] } }

当我使用关键字 =“越南”运行查询时

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]

如何搜索包含上述单词的结果(添加类型,或做任何事情)

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

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