-
Notifications
You must be signed in to change notification settings - Fork 6
Basic Querying
Well after building your first index (hopefully), you can start looking to query it :D
First of all we need to start by creating a query builder object. In lucene terms this is pretty close to a boolean query. It does however have some bells and whistles over the traditional lucene implementation in that you can remove / replace / add additional queries and therefore re-execute the query over and over changing only one / a few parameters.
This really is as simple as...
LuceneSearch search = new LuceneSearch("path to my index");
So lets go ahead and create our first query builder.
IQueryBuilder queryBuilder = new QueryBuilder();
If we have a good idea of what our query is going to do upfront, the query builder has a constructor argument that would allow us to set that up front (similar to the Setup() method of the query builder).
IQueryBuilder queryBuilder = new QueryBuilder(x => x.Term("field1", "value1"), x.WildCard("field2", "value2"));
This is analogous to any of the following:
IQueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.Setup(x => x.Term("field1", "value1"), x.WildCard("field2", "value2"));
IQueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.Term("field1", "value1");
queryBuilder.WildCard("field2", "value2");
For more on query types please look at the Query Types
So now we have set up how we want to query the data, we now need to actually perform the query. This can be achieved by passing the querybuilder to the LuceneSearch object
LuceneSearchResult result = search.Execute(queryBuilder))
The LuceneSearchResult object is IEnumerable, so you could simply enumerate through its elements. However, you can also use either of the provided methods.
// we can then either get paged
List<Document> documents = result.GetPagedDocuments(start, end);
// or top results
List<Document> documents2 = result.GetTopDocuments();
So the final code may look something like this.
IQueryBuilder queryBuilder = new QueryBuilder();
// syntax can be done in several ways - including any of the following:
queryBuilder.Term(BBCFields.Title, "africa");
queryBuilder.Where(x => x.Term(BBCFields.Title, "africa"));
// if you have a group of queries you want to set up - maybe use the Setup method
queryBuilder.Setup
(
x => x.Term(BBCFields.Title, "africa"),
x => x.Term(BBCFields.Title, "wildlife"));
LuceneSearch search = new LuceneSearch(GeneralConstants.Paths.BBCIndex)
LuceneSearchResult result = search.Execute(queryBuilder);
// we can then either get paged
List<Document> documents = result.GetPagedDocuments(start, end);
foreach(Document document in documents)
{
Console.WriteLine("Title: " + document.GetValues(BBCFields.Title).FirstOrDefault());
}
// or top results
List<Document> documents2 = result.GetTopDocuments();
foreach(Document document2 in documents2)
{
Console.WriteLine("Title: " + document2.GetValues(BBCFields.Title).FirstOrDefault());
}
Hopefully pretty simple to understand.
Well - you know how to query - you might want to consider Sorting your data.