简体   繁体   中英

RavenDB searching multiple words

I want to search documents in RavenDB and make the search results more precise for each word searched.

This is my current code (simplified). For each iteration in the loop I want to make and "and" clause. How is that possible?

At the moment I just get more results for each word, but I want to get less results per word.

using (var session = this.ravenSession.Store.OpenAsyncSession())
{
    var query = session.Query<TestDocument, TestIndex>();

    foreach (string word in search.Split(' '))
    {
        query = query.Search(x => x.Brand, word, boost: 5)
                     .Search(x => x.Model, word, boost: 3)
                     .Search(x => x.Variant, word)
                     .Search(x => x.Color, word);
    }
}

Solved : I had to use a DocumentQuery and make sub clauses for each loop.

using (var session = this.ravenSession.Store.OpenAsyncSession())
{
    var query = session.Advanced.AsyncDocumentQuery<TestDocument>();

    var words = search.Split(' ');

    for (int i = 0; i < words.Length; i++)
    {
        query.OpenSubclause();

        query = query.Search(x => x.Brand, words[i]).Boost(5).OrElse()
                     .Search(x => x.Model, words[i]).Boost(3).OrElse()
                     .Search(x => x.Variant, words[i]).OrElse()
                     .Search(x => x.Color, words[i]);

        query.CloseSubclause();

        if (i < (words.Length - 1))
        {
            query.AndAlso();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM