Multiple Search terms using Ruby Searchkick - ruby-on-rails

I have set everything up using ElasticSearch and SearchKick, however, I cannot get it to work quite like I want to. I need to have multiple searches.
My model, Car, has two attributes such as "Make" and "Year". I can do this:
Car.search( query: { match: {make: { query: "toyota" } }} )
How do I also find matches that belong to Toyoto but also belonging to a specific year, say 2012.
Thank you

Assuming that year is either an attribute of your Car model or defined explicitly in your search_data config this should work:
Car.search "toyota", where: { year: 2012 }

This will returns cars in which make is "toyota" AND year is 2012
Car.search "*", where: { make: 'toyota', year: 2012 }
For make is "toyota" OR year is 2012
Car.search "*", where: { or: [[{make: 'toyota'},{year: 2012}]] }

Related

Elasticsearch match field=value (not contains)

I have problem while searching through elasticsearch.
I have index product with fields title and gender
When I make query with default_field: title I need to get results only with gender=male (not female or others)
query: dress AND gender:male
Results contain both genders: male and female and male,female
It seems to me that gender:* search all which contains male, but not full match of value. How to do the query right?
I use it through Ruby on Rails
Product.search({
query: {
query_string: {
query: query,
default_field: "title"
}
},
size: per_page,
sort: [ _score: { order: relevance } ]
})
Is gender a keyword data type? I suspect that you left/set the default mapping to the gender field (i.e., text + keyword subfield). In this case, try the following query: dress AND gender.keyword:male
According with this I just need to put value in double quotes..
query = '(dress) AND (gender:"male")'
do not forget to escape them if needed "gender:\"male\""

Searchkick Order by price Or Name returns incorrect order of results

I'm using searchkick to for searching and ordering products on my rails application everything is working fine except
search("*", order: { price: {order: :desc}}
// or
search("*", order: { price: :desc})
// name
search("*", order: { name: {order: :desc}}
//or
search("*", order: { name: :desc})
both of the above return correct results but with incorrect order
my price & name mapping is
"name"=>
{"type"=>"keyword",
"fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_start"=>{"type"=>"text", "analyzer"=>"searchkick_word_start_index"}},
"ignore_above"=>30000},
"price"=>{"type"=>"float"}
How can i fix this so i get correct order of results ?! i.e by price desc ?
You can try to debug what's going on with:
Model.search("*", order: {price: :desc}, debug: true)
And
Model.search("*", order: {price: :desc}, explain: true).response
Source
Once possibility is your search index is not in sync with your database. Try doing a full reindex (Model.reindex) to see if that fixes it.

How to group records by day in Rails using Postgres

So I've found these questions:
How to group and count by day in Rails in Postgres?
Grouping by week/month/etc & ActiveRecord?
How do I group by day instead of date?
And this gem: https://github.com/ankane/groupdate
I'm looking to group records by day, in a format like this:
[
{ "2016-03-16" => [Record1, Record2, Record3] },
{ "2016-03-17" => [Record1, Record2] },
{ "2016-03-18" => [Obj1, Obj2] }
]
I've been able to get this format using this code:
def group_by_criteria
created_at.to_date.to_s
end
list.group_by(&:group_by_criteria).map {|k,v| { k => v} }
However, as explained in other questions, this is not very efficient for a large number of records, I think I should be using grouping from the db, but I'm not sure how to get the format I'm looking for, I tried something like this but I don't get what I'm expecting:
list.order("date_trunc('day', created_at) DESC).map{ |k, v| { k => v }}
How can I group records by day from the db like this?
Because you ultimately do want to load all records into memory, your first solution (grouping in Ruby) is actually the best you can do. Leveraging SQL grouping could help optimize if you wanted to just, say, get a count of records in each group, or even a comma-separated list of record names, but -- since you actually want the records -- there's no way to get around the fact you'll simply have to fetch all the records.

Neo4J - Linked List: returning results without order

I'm using Neo4j server and I have to resolve this use case:
I have a linked list of comments and I want to get the most recent comments with their author and the name of the author who like them, so my linked list is the following one:
(Resource)-[:COMMENTS]->(Comment)-[:NEXT]->(Comment)-...-[:NEXT]->(Comment)
(User)-[:LIKES]->(Comment)
(User)-[:MAKES]->(Comment)
I want Neo4j to retrieve for every Comment:
{text: comment_text,
date: comment_date,
author: comment_author,
likes: [users_who_like_the_comment] }
I made this query:
MATCH (r)-[:COMMENTS|NEXT*0..3]->(cc:Comment)<-[:LIKES]-(a:User)
WHERE id(r)=6468
WITH r,a, collect(cc) AS Likes
MATCH r-[:COMMENTS|NEXT*0..3]->(c:Comment), (u:User)-[:MAKES]->c
WITH Likes,c,u,a
RETURN {text: c.text, date: c.date, author: u.name, likes: CASE WHEN (c) in Likes THEN collect(a.name) END } as Comments
The problem is that I get the correct result but without order, which should be provided by default in a linked list, so I guess I'm doing something wrong but I'm not able to figure out what...
How about this:
MATCH (r)-[:COMMENTS]->()-[NEXT*0..3]->(c:Comment)<-[:MAKES]-(u:User)
WHERE id(r)=6468
OPTIONAL MATCH (c)<-[:LIKES]-(a:User)
RETURN {text: c.text, date: c.date, author: u.name, likes: collect(a.name)} as Comments
if that doesn't keep the order, you can change it to:
MATCH path=(r)-[:COMMENTS]->()-[*NEXT*0..3]->(c:Comment)<-[:MAKES]-(u:User)
WHERE id(r)=6468
OPTIONAL MATCH (c)<-[:LIKES]-(a:User)
WITH {text: c.text, date: c.date, author: u.name, likes: collect(a.name)} as Comments,
length(path) as len
ORDER BY len ASC
RETURN Comments
And for good measure here is everything as a GraphGist

How to sort a form field values through generator file using symfony?

how are you all guys?
I want to sort(in ascending or descending order) a form fields through generator.yml in symfony. I mean I have 2 tables Events and Members. Relation between these two tables are many to many. What I want is that when I go to add/edit an event, members list should be sorted by their names.
I tried to use:
config:
actions: ~
fields:
title: { help: Title of the event ,label: Event Name *}
event_datetime: { help: Set the date and time of event ,label: Date time *}
details: { help: Details related to event ,label: Details *}
venue_id: { help: Select venue }
is_visible: { help: Select is visible or not}
members_list: { help: List of members }
slug: { help: User Friendly URL,label: User Friendly URL }
sort: [mmebers_list, asc]
But it does not work successfully.
any suggestion please?
Thanks
Try Adding this to your schema.yml
Member:
actAs:
Timestampable:
Sluggable:
unique: true
fields: [name]
canUpdate: true
options:
orderBy: name ASC
This will sort all the members lists globally in your application
I had a similar problem, but I use sfGuardUser plugin and I don't want to change their schema. I had to change action files and add:
public function executeNew(sfWebRequest $request)
{
parent::executeNew($request);
$sql = Doctrine_Query::create()->from('sfGuardUser u')->innerJoin("u.Profile p")->where('u.is_super_admin = ?',false)->orderBy('p.lastname');
$this->form->getWidget('user_id')->setOption('query',$sql);
}
You need to pass query which symfony use to get data to field.

Resources