How to do a Full Text Search when the query word contains a space?

625 views
Skip to first unread message

Justin A

unread,
Sep 25, 2014, 9:43:43 AM9/25/14
to rav...@googlegroups.com
Reading up on Searching in RavenDB...

This method allows you to pass a few search terms that will be used in searching process for a particular field. Here is a sample code that uses Search extension to get users with name John or Adam:
?
1
users = session.Query<User>("UsersByName").Search(x => x.Name, "John Adam").ToList();
Each of search terms (separated by space character) will be checked independently. The result documents must match exact one of the passed terms.

Simple.

But what happens if I wish to search for "John" and "Adam" and "Jane Citizen" ? (and Jane Citizen is a value in the 'terms')

I tried doing this..

var query = "John adam \'jane citizen\""
var results = asyncDocumentSession.Query<Index.Result, Index>().Search(x => x.Query, query).ToListAsync();
(NOTE: not doing a wildcard search (ie. starts with) but an exact search against the terms).

but that didn't return the result i was after.

any suggestions, please?

-J-

Chris Marisic

unread,
Sep 25, 2014, 10:07:52 AM9/25/14
to rav...@googlegroups.com
var terms = search.Split(' ', remove empty entries)

var query =...

foreach(term in terms)
query = query.Search(x=> x.Query, term, SearchOptions.And)

query.ToList()

You may also need to use an OpenSubClause/CloseSubclause depending on the other things your query has to do.

Kijana Woodard

unread,
Sep 25, 2014, 10:41:48 AM9/25/14
to rav...@googlegroups.com
You should write up a test here so we can see what your going after.

"John" and "Adam" and "Jane Citizen"

I assume you actually mean OR. And would mean Query contains all of those values.
I think you're going to have trouble using the standard analyzer here since it breaks things down into "words" based on spaces.

If you want an exact match on say, name, and then a full text search on say Description, Bio, Title, etc, you should look into Boosting.


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

Kijana Woodard

unread,
Sep 25, 2014, 10:43:28 AM9/25/14
to rav...@googlegroups.com
I should also say, iirc, you can match 
.Where(x => x.Name == "Jane Citizen")
.Search(x => x.Query, query)

Justin A

unread,
Sep 26, 2014, 3:20:32 AM9/26/14
to rav...@googlegroups.com

Figured it out!!

trick was to put the query term inside some quotes BUT THEN make sure the query is is do a raw query..


eg.

...
.Search(x => x.Name, query, escapeQueryOptions: EscapeQueryOptions.RawQuery)
...

so then i can do queries like

fred* bob* "jane smith*"

and works nicely!

Yes, this means there's a risk people can do things like f?ed, etc...

thanks folks!


Justin A

unread,
Sep 26, 2014, 3:21:55 AM9/26/14
to rav...@googlegroups.com

so then i can do queries like

fred* bob* "jane smith*"



btw - that means

words that start with Fred -or- words that start with bob -or- words that start with Jane Smith ...

woot! 
Reply all
Reply to author
Forward
0 new messages