How to search product data by id and name in one query - ruby-on-rails

I have a index named product in elastic-search with field id, name etc..
I want to search products by id or name but my id field is integer and name is a text, I tried following but getting error while searching by name.
error type":"number_format_exception","reason":"For input string:
\"test\""
def self.search_by_name_or_id(query)
__elasticsearch__.search({
query: {
bool: {
must:{
multi_match: {
query: query,
fields: ["id", "name"]
}
}
}
}
})
end

Problem
Exception is clear, that you are trying to search test which is a String in the integer field and elasticsearch is not able to convert test in integer, instead of test if you had search for 10 or 100, then elasticsearch would convert it into integer and would not throw the exception.
Solution
You are trying to mix 2 things here, I am not sure about your design but if your id field can contain pure numbers i.e. integers, then it's not possible to achieve in a single query the way you are doing.
If you can convert your id field to String, then multi_match query would work perfectly fine, otherwise, you need to first check in your application, that search term can be converted to number or not, i.e. 10 or 100 would work fine but test10 or test100 would not and anyway there is no point of searching these terms in id field as it won't be present as its defined as integer in ES and ES would reject documents containing these terms during indexing time only. So based on your application code check you can construct the ES query which may or may not include the id field in multi-match.

Related

How do I query a list of users?

If I want to query a list of users, I want to dynamically pass in the parameters, for example, can only query according to username, or according to the combination of username and userType conditions to query, I do not know how to use typeORm to write
I guess what you are looking for is find options. Link to official documentation: TypeORM - Find Options
repository.findOne(id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
findOne function takes in two parameters. First one defines logic to how you want the record to find, by id or its column value. Second parameter lets you fetch the relations if you have any with the specific entity.

How to filter out non-null path between nodes in Neo4J/Cypher

My current graph monitors board members at a company through time.
However, I'm only interested in currently employed directors. This can be observed because director nodes connect to company nodes through an employment path which includes an end date (r.to) when the director is no longer employed at the firm. If he is currently employed, there will be no end date(null as per below picture). Therefore, I would like to filter the path not containing an end date. I am not sure if the value is an empty string, a null value, or other types so I've been trying different ways without much success. Thanks for any tips!
Current formula
MATCH (c2:Company)-[r2:MANAGED]-(d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
WHERE r.to Is null
RETURN c,d,c2
Unless the response from the Neo4j browser was edited, it looks like the value of r.to is not null or empty, but the string None.
This query will help verify if this is the case:
MATCH (d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
RETURN DISTINCT r.to ORDER by r.to DESC
Absence of the property will show a null in the tabular response. Any other value is a real value of that property. If None shows up, then your query would be
MATCH (c2:Company)-[r2:MANAGED]-(d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
WHERE r.to="None"
RETURN c,d,c2

Way to order alphanumeric string

I have a series of model objects with the following values on a column:
2018-A-1
2018-A-10
2018-A-2
2018-A-100
2018-A-11
2018-B-1
2018-B-10
2018-B-2
2018-B-100
2018-A-11
I would like to query the db and get the result in alphanumeric order. I would like to obtain the result in this order:
2018-A-1
2018-A-2
2018-A-10
...
Instead, I obtain the following order:
2018-A-1
2018-A-10
2018-A-100
...
Any smart way to achieve that result directly with active record?
Here I'm re-stating your example data as an array:
example_array = %w{2018-A-1 2018-A-10 2018-A-2 2018-A-100 2018-A-11 2018-B-1 2018-B-10 2018-B-2 2018-B-100 2018-A-11}
It's not perfectly clear to me what order you're trying to get. If you are trying to sort based only on the digits following the final hyphen, this should work:
example_array.sort_by{|e| e.split("-").last.to_i }
If you also intend to include the letter before the final number, perhaps this is what you want:
example_array.sort_by{|e| [e.split("-")[-2], e.split("-").last.to_i] }

Searching for matching records in a field saved as a hash in PostgreSQL?

I have an active record SearchSuggestion that has a title:text field for storing values for different languages in a single hash that looks like so:
#<SearchSuggestion :id 1, link: "/search/shoes", title: {"en"=>"Women's shoes", "ko"=>"여성 신발", "ru"=>"Женская Обувь", "fr"=>"Chaussures De Femme"}>
and I'm fetching records that match the given input in the following way: all of the given words contained in all records' title fields, starting only from the start of each word the following way:
query = params[:q].split(" ").map{|s| s.prepend('(?:.*\m')}.map{|s| s.concat(')')}.join('')
SearchSuggestion.where("title ~* ?", query).limit(10).pluck(:title)
The problem is that this gives me matches from the whole field, and I want just the ones for the current selected language. So if I have languages that are using the same alphabet - I get unrelated results e.g - if I start typing "cha" this will return "Women's shoes" by finding the French suggestion as a match. Also if I type "en" - I get all of the existing records as a result, because they match the key for "en" language.
Is there any way I can specify the hash's key that I only want to search within?

Grails searcheble don't use field during fulltext search

Domain (just simple example):
class House {
String address
String region
Long price
static searchable = { only = ['address', 'price', 'region'] }
}
I want to search by address with price selector
Search query is:
"${address} AND price: [100 TO 1000]"
But if address='500' in search result will be Houses with price 500. I need to find house for address only by House.address + House.region
If I understood, you want to use the value of ${address} to query only on the address and region attributes.
In that case, based on the "complex" query string example at http://grails.org/Searchable+Plugin+-+Searching+-+String+Queries, your query string could be:
"+(address:${address} OR region:${address}) price:[100 TO 1000]"
Matches must have a ${address} value for address or a ${address} value for region, and have a value from 100 to 1000 for price.
Note: your input variable has the same name of an entity attribute, "address", wich can cause some confusion when trying to understand query strings using them both. Though not needed, you might want to change that, for easier readable query strings.

Resources