Well after building your first index (hopefully), you can start looking to query it 😀
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.
Creating the Search
This really is as simple as…
LuceneSearch search = new LuceneSearch("path to my index");
Creating the Query
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
Executing the Query
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))
Viewing the Query
The querybuilder object actually creates a BooleanQuery Lucene.Net object, so if you wish to see what it has created (mainly for copying and pasting into Luke or your Lucene index tool of choice), simply call queryBuilder.Build().ToString().
Handling the results
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.
What Next?
Well – you know how to query you will want to know about the types of query.