Page 1 of 2

Text search problem

Posted: Fri Dec 13, 2013 2:57 pm
by Adam Garbinski

Hello

I am trying to improve search functionality in my app. It works fine in case of single expression scenario where user enters a single expression and the item that contains that expression. I want to change it so it can also work in the following scenario:

Item name in the database could be like this:

"Delicious dessert with banana and milk"

User' search input can be like this:

"banana milk"

So I want my search to return to the user all items that contain in their name field expressions "banana" and "milk".

I realize there may ways to achieve this (there is a text search functionality in mongodb for example), but I would appreciate your recommendation on what would be the simplest approach to solve this problem.

Right now my mapping looks like this:

Image

and the JS mapping from search text field to where is as follows:

{
if (localStorage.ShowBlogArtOnly == 'on')
{
return '{$and: [{"Name":{"$regex":"' + value + '","$options":"im"}}, {"SourceID":"01"}]}';
}
else
{
return '{"Name":{"$regex":"' + value + '","$options":"im"}}';
}
}
else
{
return '{"id": {"$exists":false}}';
}


Text search problem

Posted: Fri Dec 13, 2013 5:52 pm
by Maryna Brodina

Hello! If you need to search with specific words try to separate entire phrase and add some words separated with | symbol in regex prereturn '{"Name":{"$regex":"' + value.replace(/ /g, "|") + '","$options":"im"}}';/pre


Text search problem

Posted: Fri Dec 13, 2013 9:55 pm
by Adam Garbinski

It works nice :-) Maryna however I needed the AND operator between phrases. I googled a bit and found a "positive lookahead" REGEXP syntax so I have modified your solutions so it is now like this:

return '{"Name":{"$regex":"(?=.' + value.replace(/ /g, ")(?=.") + ')","$options":"im"}}';

and it WORKS :-) exactly as I wanted.

So many thanks Maryna for putting me on the right track!
Cheers.


Text search problem

Posted: Mon Dec 16, 2013 4:33 am
by Ketan Patil

this is great solution to search string


Text search problem

Posted: Mon Jan 13, 2014 4:58 am
by bahar.wadia

I have a similar problem.

However, in my case I want to return results by matching the first three characters of any word in a sentence.

eg This is my table, and it is brown.

So, using $regex 'bro' would return true
using $regex 'Thi' would return true
using $regex 'ble' would return false

Thank you for your help in advance.


Text search problem

Posted: Mon Jan 13, 2014 12:59 pm
by Maryna Brodina

Hello! Sorry, not sure I understand. Could you give more details? What do you specify in search field? What records do you have in collection? What records are supposed to be choosen and why?


Text search problem

Posted: Mon Jan 13, 2014 8:34 pm
by bahar.wadia

I have a collection that holds strings (sentences) in Rows. I want my query to return all Rows where any word/words match the first three characters of the sub string I provide through the $regex.

For example:

Row 1 - Hello World!
Row 2 - The world is a great place.
Row 3 - The earth is a planet
Row 4 - Appery is great

I want Rows 1 and 2 returned when I pass "wor"

I want Rows 2 and 3 returned when I pass "pla"

I want Row 1 returned when I pass "hel"

I want Rows 2 and 4 returned when I pass "gre"

How do I do this?

Thanks


Text search problem

Posted: Mon Jan 13, 2014 9:39 pm
by Kateryna Grynko

Hi Bahar,

For the first example, if a field is called 'fieldName' you can use request parameter 'where': pre{"fieldName":{"$regex":"wor*","$options":"im"}}/preHere we search by value 'wor' words 'World' and 'world'.


Text search problem

Posted: Tue Jan 14, 2014 3:28 am
by bahar.wadia

You are the best.

Thank you so much!


Text search problem

Posted: Tue Jan 14, 2014 3:49 am
by bahar.wadia

Hi Katay,

This works, but it searches for any sub string. Meaning it would return Rows 1 and 2 for 'orl', which is not what I want. I only want to return Rows that have words (anywhere in the sentence) beginning with the three characters I provide.

Thanks