danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

A quick introduction to Lucene Searches in Cloudant using MyCouch

Daniel WertheimDaniel Wertheim

This week the “MyCouch.Cloudant NuGet” package was released. Since Cloudant is almost 100% compatible with CouchDb, the plain MyCouch package is working nicely with Cloudant. However, there are some great features in Cloudant which are not implemented in CouchDb, hence the birth of MyCouch.Cloudant. The first feature out, is support for Searches. Tag along and I’ll show you how to use it.

Searches is a way for you to run ad-hoc queries against your Cloudant data, using Lucene. The way you do this is by creating a search index and then you start querying it. Simple! I know. Except from passing expressions, you can sort the output, you can limit the size of the result set and use this in conjunction with a bookmark to page through the data. But lets stop talking and lets see how this is consumed in MyCouch.

Did you catch the coding error? I did misspell omnivore hence one query was empty in the response. The full code (corrected) looks like this:

var uri = new MyCouchUriBuilder("https://myaccount.cloudant.com/")  
  .SetDbName("animaldb")
  .SetBasicCredentials("key", "pwd")
  .Build();

using (var client = new CloudantClient(uri))  
{
  var elephant = client.Searches.SearchAsync("views101", "animals",
    q => q.Expression("elephant"));

  var omnivores = client.Searches.SearchAsync("views101", "animals",
    q => q.Expression("class:omnivore"));

  var mediumSizedHerbivores = client.Searches.SearchAsync("views101", "animals",
    q => q.Expression("min_length:[1 TO 3] AND diet:herbivore").Sort("min_length"));

  Task.WaitAll(
    QuichSearchDemo.DumpAnimals(elephant),
    QuichSearchDemo.DumpAnimals(omnivores),
    QuichSearchDemo.DumpAnimals(mediumSizedHerbivores));
}

If you are looking for more info about MyCouch, just head over to the GitHub repository or ping me and I’ll attend your question as soon as possible.

Cheers,

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments