0
votes

I use chewy gem elasticsearch .

I have LocationsIndex, mapping :

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

when i query:

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

data sample :

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 10, "max_score" : 1.0, "hits" : [ { "_index" : "locations", "_type" : "location", "_id" : "131", "_score" : 1.0, "_source":{"name":"Việt Nam","address":"Việt Nam","locations":{"lat":16.9054,"lon":106.576}} }, { "_index" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_id" : "133", "_score" : 1.0, "_source":{"name":"Ha Noi","address":"Ha Noi, Việt Nam","locations":{"lat":16.4674,"lon":107.591}} }, { "_index" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_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" : "locations", "_type" : "location", "_id" : "130", "_score" : 1.0, "_source":{"name":"Việt Nam","address":"Việt Nam","locations":{"lat":16.9054,"lon":106.576}} }, { "_index" : "locations", "_type" : "location", "_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}} } ] } }

when i run query with keyword = "viet nam"

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

but i when run query with keywork = "thua thien hue"

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

Same with keywork = "hue"

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

how search results that contain the above word (add type, or do anything)

1

1 Answers

0
votes

You have an error when declaring field address. You are declaring address.address instead of address.folded. In other words, the declaration should be:

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