ElasticSearch: A query that allows nil parameters - ruby-on-rails

So i have the below module in an ElasticSearch concern for my Model in rails.
This is working, but how do I make each of the bool query(must, must_not, filter) accept nil or empty parameters?
Say if I pass an empty query_string it would get all the documents.
Then when I pass an empty size parameter it will return all sizes.
module ClassMethods
def home_page_search(query_string, size, start_date, end_date)
search({
query: {
bool: {
must: [
{
multi_match: {
query: query_string,
fields: [:brand, :name, :notes, :size_notes]
}
}
],
must_not: [
range: {
unavailable_dates: { gte: start_date, lte: end_date }
}
],
filter: [
{ term: { size: size } }
]
}
}
})
end
end

I solved a similar problem by constructing the query string on more of an as-needed basis, so I only included a clause if there was a search term for it. The query I sent to Elasticsearch only included the terms that were actually set by the user. For example:
if size.present?
query[:query][:bool][:filter] = { term: { size: size } }
end
(assuming the correct representation of the query, etc.)

Related

How to use bool to limit Elasticsearch query results?

In my Rails app I have 2 models: User(id) & Document(id,user_id, document_title,document)
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['document_title^10', 'document']
}
},
}
end
I'm using the above search query which works great for return results across the entire table. The problem is, the results are not limited to the current_user. I'm trying to update the search method to only return results for the current_user. Per the docs, I'm doing:
def self.search(query, user_id)
__elasticsearch__.search(
{
bool: {
filter: ["user_id", user_id]
},
query: {
multi_match: {
query: query,
fields: ['document_title^10', 'document']
}
},
}
end
However, that is erroring with:
[400] {"error":{"root_cause":[{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [bool]","line":1,"col":2}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"documents","node":"52GAD0HbT4OlekjesTZY_A","reason":{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [bool]","line":1,"col":2}}]},"status":400}
I'm not sure what docs you are looking at but that query isn't right: the multi match query should be in the must clause of the bool query.
{
query: {
bool: {
must: [{
multi_match: {...}
}],
filter: [{
term: {user_id: user_id}
}]
}
}
}

Elasticsearch sort option not supported

I'm using elastic search in Rails. I am trying to sort a list of customers by their total dollars spent descending. This is my ruby code:
query = {
bool: {
filter: {
term: { store_id: store.id } # Limits customers by current store
}
}
}
sort = {
sort: { "total_spent": { order: "desc" }}
}
response = Contact.search(query: query, sort: sort)
This returns with an error of sort option [total_spent] not supported I've tried with other fields to make sure it wasn't just something wrong with the total_spent field. Thanks.
I'm not really sure, but I think this may be related to incorrect usage of the ES::DSL.
What happens when you try this:
query = {
bool: {
filter: {
term: { store_id: store.id } # Limits customers by current store
}
}
}
sort = {
sort: [{ "total_spent": { order: "desc" }}] #https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
}
response = Contact.search(query, sort)
We can sort specific to the field, refer https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html.
so we can use like,
query = {
bool: {
filter: {
term: { store_id: store.id } # Limits customers by current store
}
},
sort: { total_spent: { order: :desc }}
}
response = Contact.search(query)

ElasticSearch query date capability scripting

A little background....I'm converting an existing API to leverage elastic search for performance gains.
It's a rails app that's using active record. Here is the query where condition that we're trying to do in elasticsearch:
where("start_date BETWEEN :start_at AND :end_at
OR end_date BETWEEN :start_at AND :end_at
OR :start_at BETWEEN start_date AND end_date
OR :end_at BETWEEN start_date and end_date", start_at: start_at.to_date, end_at: end_at.to_date)
The first half of that where clause is easy to replicate. Here's how that part looks:
Model.search(
query: {
bool: {
should: [
{
range: {
start_date: {
gte: '2015-12-01',
lte: '2015-12-25'
}
}
},
{
range: {
end_date: {
gte: '2015-12-01',
lte: '2015-12-25'
}
}
}
],
minimum_should_match: 1
}
}
)
I'm just not sure how to implement the 2nd part of that where clause. I've attempted using scripting but I'm having issues converting the dates to the proper formats so that they can be compared.
For example here was something I tried:
filter: {
script: {
script: {
inline: "doc['start_date'] > start_on",
params: {
start_on: Date.parse('2015-12-01')
}
}
}
}
When I do this I get this error:
Cannot compare org.elasticsearch.index.fielddata.ScriptDocValues$Longs with value '[1449446400000]' and java.lang.String with value '2015-12-01'"
I've also tried this:
script: "Date.parse('yyyy-MM-dd', '2015-12-01').getTime() >= doc['start_date']"
I get this error:
org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Long
I'm just not sure how to get the data types to match so that I can compare the dates.
I wish I could do something like this, but its not possible of course:
range: {
'2015-12-01': {
gte: start_date
}
}
Any help on this would be greatly appreciated. Thanks!
Try this:
filter: {
script: {
script: {
inline: "new java.util.Date(doc['start_date'].value) > new java.util.Date(2015-12-01)"
}
}
}
A co-worker sent me this: https://stackoverflow.com/a/325964, so I ended up with a much simpler approach:
Model.search(
query: {
bool: {
must: [
{
range: {
start_date: {
lte: '2015-12-25'
}
}
},
{
range: {
end_date: {
gte: '2015-12-01',
}
}
}
]
}
}
)

Find model with part of title using ElasticSearch / Rails

There is the following Post model:
class Post < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def self.search query
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['title']
}
},
filter: {
and: [
{ term: { deleted: false } },
{ term: { enabled: true } }
]
}
}
)
end
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, analyzer: 'english'
end
end
end
Post.import
I have one Post with 'Amsterdam' title. When I execute Post.search('Amsterdam') I will get one record, all is good. But if I execute Post.search('Amster') I will get no records. What do I wrong? How can I fix it? Thanks!
OS - OS X, ElasticSearch I installed using Homebrew
You will have to use nGram tokenizer, in order to create a partial text search. A very good example of how to do this can be found here. That said, I would be very careful with nGram, as it can often turn up unrelated results.
This is because the substring "mon" is contained within all of the strings: "monkey", "money", and "monday". All of which are unrelated.
Alternatively (What I would do.)
You could try making it a fuzzy search. However, the max distance with fuzzy search is only two, which still doesn't return anything in your example. However, it tends to return relevant results.
The example I found: How to use Fuzzy Search
# Perform a fuzzy search!
POST /fuzzy_products/product/_search
{
"query": {
"match": {
"name": {
"query": "Vacuummm",
"fuzziness": 2,
"prefix_length": 1
}
}
}
}

Multiple elasticsearch filters

I am using Tire for rails to integrate elasticsearch. This bit is quite confusing and I want to make sure I'm doing this right.
Is this how I apply multiple filters? I'm basically trying to check 'mixtape_id IS NULL AND artist_id IS NOT NULL'
def self.search(query)
tire.search() do
query { string query }
filter :exists, { field: 'artist_id' }
filter :not, { exists: { field: 'mixtape_id' } }
end
end
Here is my second attempt, still doesnt appear to work
def self.search(query)
tire.search(load: true) do
query { string query }
filter :and, [
{ exists: { field: 'artist_id' } },
{ not: { exists: { field: 'mixtape_id' } } }
]
end
end
Thanks
I mostly had it working the whole time, I stupidly forgot to force reindexing each time though sigh. Here is some cleaned up code that takes advantage of the missing filter.
def self.search(query)
tire.search load: { include: { artist: :attachments } } do
query { string query }
filter :and, [
{ exists: { field: 'artist_id' } },
{ missing: { field: 'mixtape_id' } }
]
end
end

Resources