How to Implement Full text Search("Contains" & "StartsWith") together using raven db?

700 views
Skip to first unread message

coolsan...@gmail.com

unread,
Aug 30, 2015, 4:15:54 AM8/30/15
to RavenDB - 2nd generation document database
I am new to raven db so guys please help me implementing full text search which will work like CONTAINS & STARTS-WITH. I am using raven db 3.0

Following is my scenario:

I have one column in database as NAME and it has values with spaces. Ex. let's say, "The quick brown fox jumps over the lazy dog".

CASE1:

So what i want to perform is, as a user when i type "dog" or "lazy" or "fox"(any single word from above statement in a search box it should return me "The quick brown fox jumps over the lazy dog"). This is the CONTAINS scenario.

CASE2:

Another case is STARTS-WITH, as a user when i type "The quick "(ALSO NOTE THE SPACE AT THE END) or "The quick brown"(NO SPACE HERE ) or "The quick brown fox" or "The quick brown fox jumps ", search should return me whole text "The quick brown fox jumps over the lazy dog" from the db.


Please refer my code snippet:

Index definition:
After setting this.map i am doing this,
this.Indexes.Add(x => x.Name, FieldIndexing.Analyzed)

Linq query:
var resultSet = from temp in Query<Model>(Index.NAME) .Where(x => x.Name.StartsWith(RavenQuery.Escape(Searchterm))) select new { temp.Name }

NOTE:
Above code is working for CASE1 but not for CASE2. When i change fieldindexing to
FieldIndexing.Default in above index
defination and when i remove
RavenQuery.Escape() from above linq query, CASE2 starts working but CASE1 not works.
So Somehow i need to get both case working.

I spent lot of time going through various posts/articles but unable to figure out the solution. I already tried below approaches:
  1.  .Search() tried all search options but nothing helped :(
  2.  .In() (When i tried this, query generated was something like @<Name>:"The quick", i was getting @<Name> field is not indexed error.)
  3.  Wildcard search, used Searchterm=string.format("*{0}*",Searchterm)
  4. also tried Searchterm=Searchterm.replace(" ",@"\ ") as a separately and in .Search method as a search term.
  5. tried using almost all analyzers from raven studio & code. 

Grisha Kotler

unread,
Aug 30, 2015, 8:02:41 AM8/30/15
to rav...@googlegroups.com
Hi,

For case 1: set the name field as analyzed.
For case 2: index the field under different name: NotAnalyzedName and don't apply any analyzer to it.

LInq query:
var resultSet = from temp in Query<Index.Result>(Index.NAME)
.Where(x => x.NotAnalyzedName.StartsWith(Searchterm))
select new { temp.Name }

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Grisha Kotler l RavenDB Core Team Developer Mobile: +972-54-586-8647

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

RavenDB paving the way to "Data Made Simplehttp://ravendb.net/


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

coolsan...@gmail.com

unread,
Sep 5, 2015, 10:53:55 AM9/5/15
to RavenDB - 2nd generation document database
This work like a charm!!!!!! Thanks a ton Grisha Kotlar
Reply all
Reply to author
Forward
0 new messages