1

I have an object with a string field stored in a RavenDB. For example:

public class SomeObject
{
    public string SomeText = "hello world";
}

I want to be able to check both of the following in the same query:

  1. Checking if the entire string starts with a certain value. In this case:
    • "hell" would match
    • "hello" would match
    • "hello w" would match
    • "world" would not match
    • "world hello" would not match
  2. Checking if the string contains certain terms. Assuming they are space separated, in this case:
    • "hell" would not match
    • "hello" would match
    • "hello w" would not match
    • "world" would match
    • "world hello" would match

What is a good way to setup the indexing (I assume?) to be able to do this in the same query?

Edit: Clarified that I want to be able to check both things in the same query.

Linus
  • 3,254
  • 4
  • 22
  • 36

2 Answers2

1

Have a look at here. I guess you could do a combination of a Where(n => n.StartsWith(...)) and Search(...) query.

coder0815
  • 219
  • 2
  • 8
  • I'm trying to understand this conceptually, please tell me if I'm wrong. If I, as per the link, create an index `documentStore.DatabaseCommands.PutIndex("SomeObjectsBySomeText", new IndexDefinition { Map = "from someObject in docs.SomeObjects select new { someObject.SomeText }", Indexes = { { "SomeText", FieldIndexing.Analyzed } } });` it will use the StandardAnalyzer (terms "hello" and "world") instead of the LowerCaseKeywordAnalyzer (term "hello world"). If I then call `Where(o => o.SomeText.StartsWith("wor"))`, will it match or not? And why? – Linus Nov 25 '14 at 17:26
0

I ended up adding two fields to an index, both based on SomeText.

One analyzed using the StandardAnalyzer and the other using the LowerCaseKeywordAnalyzer.

This allowed me to do starts with searches using the field indexed by the LowerCaseKeywordAnalyzer. Using the field indexed by the StandardAnalyzer would have generated a hit if any of the terms starts with whatever instead of the entire string starting with it.

Searching for terms can be done in parallel using the field indexed by the StandardAnalyzer.

I don't know if this is the best solution but it solved my problem.

Linus
  • 3,254
  • 4
  • 22
  • 36