I'm using Ruby on Rails with the elasticsearch-rails gem and I am trying to use a synonyms filter. I have been following the question posted here for guidance (my implementation works as expected except for the synonym part):
https://github.com/elastic/elasticsearch-rails/issues/63
Here is my code:
settings index: { number_of_shards: 1 },
analysis: {
filter: {
synonym: {
type: "synonym",
ignore_case: true,
synonyms:[
"roller,wheel"
]
}
},
analyzer: {
synonym: {
tokenizer: "whitespace",
filter: ["synonym", "lowercase", "stop", "snowball"]
}
}
} do
mappings dynamic: 'false' do
indexes :name, analyzer: 'synonym'
indexes :status, analyzer: 'english'
#indexes :description, analyzer: 'english'
indexes :part_number, analyzer: 'english'
indexes :text, analyzer: 'english'
indexes :normal_model, type: 'nested' do
indexes :name, analyzer: 'english'
indexes :number, analyzer: 'english'
indexes :machine_type, analyzer: 'english'
indexes :normal_brand, type: 'nested' do
indexes :name, analyzer: 'english'
end
end
end
end
Here is my code to search in my controller action:
@products = Product.search(
query: {
query_string: {
#query: "*manual* AND status:\"Disabled\""
#fields: ["normal_model.name", "normal_brand.name"],
query: "*#{params[:q]}* AND status:\"Viewable On Website & Backend\""
#query: "*" + params[:q]+ "*"
}
}
)
I have records with the name field set to "wheel" but when I search "roller" I get 0 results and NO errors. I would expect to retrieve the record with the name "wheel" at this point. I have also deleted the index entirely and verified that it was deleted and recreated my index as well to ensure I wasn't just facing an indexing issue. I am not real sure what to do at this point. Any help would be appreciated.
Also here is my as_indexed_json method
def as_indexed_json(options={})
as_json(
only: [:name, :description, :part_number, :url_key, :image, :price, :shipping, :warranty, :eta, :status, :sku],
include: {
normal_model: { only: [:name, :number, :machine_type],
include: {
normal_brand: { only: :name}
}
}
}
)
end
Thanks
Update:
I've also tried adding the following code (suggested in the answer below) to my controller search action.
fields: ['name', '_all'],
query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""
I put this code in place of my original code in the search action but this still did not produce any results when I searched the word "roller". I am still able to search "wheel" and retrieve several results but I am having no luck with the specified synonym.
Update:
Here is one of the documents containing the word "wheel" in the product name field.
{
"_index" : "products",
"_type" : "product",
"_id" : "288374",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "wheel",
"description" : "This is the O.E.M. wheel for the Spirit CE800 Elliptical with a model number 800049.",
"shipping" : null,
"sku" : "58511",
"eta" : "3 to 5 Business Days",
"warranty" : "1 Year",
"part_number" : "N/A",
"url_key" : "spirit-ce800-elliptical-model-800049-lubricant",
"price" : 19.99,
"image" : "noimage-main_20837.jpg",
"status" : "Viewable On Website & Backend",
"normal_model" : {
"name" : "CE800",
"number" : "800049",
"machine_type" : "Elliptical",
"normal_brand" : {
"name" : "Spirit"
}
}
}
}
Update:
Here is my Product mapping
{
"products" : {
"mappings" : {
"product" : {
"dynamic" : "false",
"properties" : {
"name" : {
"type" : "text",
"analyzer" : "synonym"
},
"normal_model" : {
"type" : "nested",
"properties" : {
"machine_type" : {
"type" : "text",
"analyzer" : "english"
},
"name" : {
"type" : "text",
"analyzer" : "english"
},
"normal_brand" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "text",
"analyzer" : "english"
}
}
},
"number" : {
"type" : "text",
"analyzer" : "english"
}
}
},
"part_number" : {
"type" : "text",
"analyzer" : "english"
},
"status" : {
"type" : "text",
"analyzer" : "english"
},
"text" : {
"type" : "text",
"analyzer" : "english"
}
}
}
}
}
}
With your query, you are searching in the _all field. (default behaviour for query_string).
To use your synonym analyzer you must search also in the name field. Like this :
@products = Product.search(
query: {
query_string: {
fields: ['name', '_all'],
query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""
}
}
)
If params[:q]
contains "roller" you'll get your records containing the word "wheel".