RavenDB full-text search

耗尽温柔 提交于 2019-12-03 09:34:46

问题


Can you please tell how to perform simple full-text search in RavenDb. The database is stored document: Movie {Name = "Pirates of the Carribean"}. I wish that this document was found on the search phrase "Pirates Carribean" or any other combination of words.


回答1:


What you are worrying about isn't anything to do with full text - by default Lucene works on an OR basis and what you want is an AND

If I were you I'd do

 String[] terms = searchTerm.Split(" "); // Or whatever the string.split method is

and

  .Where("Name:(" + String.Join(" AND ", terms) + ")");

Your index should look something like

 public class Movie_ByName : AbstractIndexCreationTask
 {
    public override IndexDefinition CreateIndexDefinition()
    {
        return new IndexDefinitionBuilder<Movie>
                   {
                       Map = movies => from movie in movies
                                        select new { movie.Name, market.Id },

                       Indexes =
                           {
                               {x => x.Name, FieldIndexing.Analyzed}
                           }
                   }
            .ToIndexDefinition(DocumentStore.Conventions);
    }

You don't need storage, you're not requesting the data from lucene directly at any time. You might not even want an index (You might actually want FieldIndexing.Analyzed, and might get away with just using dynamic queries here)

Up to you though.




回答2:


Boris, Rob's answer has the right index, but it is a bit awkward for querying. You can do that using:

 session.Query<Movie, Movie_ByName>()
         .Search(x=>x.Name, searchTerms)
         .ToList()

That will




回答3:


Here's how I acheived an "ANDing" term search.

First, make sure that your field is indexed and analyzed:

public class MyIndex: AbstractIndexCreationTask<MyDocument>
{
    public MyIndex()
    {
        Map = docs => from d in docs
                      select new { d.MyTextField  };

        Index(x => x.MyTextField, FieldIndexing.Analyzed);
    }
}

Then query from the client:

   var query = session.Query<MyDocument, MyIndex>();

    query = theSearchText
                .Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)
                .Aggregate(query, (q, term) =>
                     q.Search(x => x.MyTextField, term, options: SearchOptions.And));


来源:https://stackoverflow.com/questions/4314545/ravendb-full-text-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!