user4342655
user4342655

Reputation:

query ravendb from web api 2 and return one document

I want to do the following using Asp.net Web API 2 and RavenDB.

  1. Send a string to RavenDB.
  2. Lookup a document containing a field called UniqueString that contain the string i passed to RavenDB.
  3. Return either the document that matched, or a "YES/NO a document with that string exists" - message.

I am completely new to NoSQL and RavenDB, so this has proven to be quite difficult :-) I hope someone can assist me, and i assume it is actually quite easy to do though i haven't been able to find any guides showing it.

Upvotes: 0

Views: 165

Answers (2)

Eric Rohlfs
Eric Rohlfs

Reputation: 1830

  1. Get the WebApi endpoint working to collect your input. This is independent of RavenDB.
  2. Using the RavenDB client, query the database using Linq or one of the other methods.
  3. After the document is retrieved you may need to write some logic to return the expected result.

I skipped the step where the database gets populated with the data to query. I would leverage the RavenDB client tools as much as possible in your app vs trying to use the HTTP api.

Upvotes: 0

Jens Pettersson
Jens Pettersson

Reputation: 1177

This has nothing to do with WebAPI 2, but you can do what you ask for using RavenDb combined with WebAPI 2.

First you need to have an index (or let RavenDb auto create one for you) on the document and property/properties you want to be indexed. This index can be created from code like this:

public class MyDocument_ByUniqueString : AbstractIndexCreationTask<MyDocument>
{
    public override string IndexName
    {
        get { return "MyDocumentIndex/ByUniqueString"; }
    }

    public MyDocument_ByUniqueString()
    {
        Map = documents => from doc in documents
                            select new
                            {
                                doc.UniqueString
                            };
    }
}

or created in the RavenDb Studio:

from doc in docs.MyDocuments
select new {
    doc.UniqueString
}

After that you can do an "advanced document query" (from a WebAPI 2 controller or similar in your case) on that index and pass in a Lucene wildcard:

using (var session = documentStore.OpenSession())
{
    var result = session.Advanced
        .DocumentQuery<MyDocument>("MyDocumentIndex/ByUniqueString")
        .Where("UniqueString: *uniq*")
        .ToList();
}

This query will return all documents that has a property "UniqueString" that contains the term "uniq". The document in my case looked like this:

{
    "UniqueString": "This is my unique string"
}

Please note however that these kind of wildcards in Lucene might not be super performant as they might need to scan large amount of texts. In the RavenDB documentation there's even a warning aginst this:

Warning

RavenDB allows to search by using such queries but you have to be aware that leading wildcards drastically slow down searches. Consider if you really need to find substrings, most cases looking for words is enough. There are also other alternatives for searching without expensive wildcard matches, e.g. indexing a reversed version of text field or creating a custom analyzer.

http://ravendb.net/docs/article-page/2.0/csharp/client-api/querying/static-indexes/searching

Hope this helps!

Upvotes: 1

Related Questions