Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Text search problem

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}}';
}

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Text search problem

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

Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Text search problem

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.

Ketan Patil
Posts: 0
Joined: Tue Dec 03, 2013 1:10 pm

Text search problem

this is great solution to search string

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Text search problem

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.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Text search problem

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?

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Text search problem

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Text search problem

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'.

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Text search problem

You are the best.

Thank you so much!

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Text search problem

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

Return to “Issues”