Searching
Tip: There are many examples of searching in the FluentApiTests
source code to use as examples/reference.
All fields (managed queries)
The simplest way of querying with examine is with the Search
method:
var results = searcher.Search("hello world");
The above is just shorthand for doing this:
var query = CreateQuery().ManagedQuery("hello world");
var results = sc.Execute(options);
A Managed query is a search operation that delegates to the underlying field types to determine how the field should be searched. In most cases the field value type will be ‘Full Text’, others may be numeric fields, etc… So the query is built up based on the data passed in and what each field type is capable of searching.
Per field
_var searcher = myIndex.GetSearcher(); // Get a searcher
var results = searcher.CreateQuery() // Create a query
.Field("Address", "Hills") // Look for any "Hills" addresses
.Execute(); // Execute the search
Range queries
Float Range
var searcher = indexer.GetSearcher();
var criteria1 = searcher.CreateQuery();
var filter1 = criteria1.RangeQuery<float>(new[] { "SomeFloat" }, 0f, 100f, true, true);
Date Range
var searcher = indexer.GetSearcher();
var numberSortedCriteria = searcher.CreateQuery()
.RangeQuery<DateTime>(
new[] { "created" },
new DateTime(2000, 01, 02),
new DateTime(2000, 01, 05),
maxInclusive: false);
Booleans, Groups & Sub Groups
TODO: Fill this in…
Lucene queries
Native Query
var searcher = indexer.GetSearcher();
var criteria = (LuceneSearchQuery)searcher.CreateQuery();
//combine a custom lucene query with raw lucene query
var op = criteria.NativeQuery("hello:world").And();
Combine a custom lucene query with raw lucene query
var criteria = (LuceneSearchQuery)searcher.CreateQuery();
//combine a custom lucene query with raw lucene query
var op = criteria.NativeQuery("hello:world").And();
criteria.LuceneQuery(NumericRangeQuery.NewLongRange("numTest", 4, 5, true, true));
Boosting, Proximity, Fuzzy & Escape
Boosting
var searcher = indexer.GetSearcher();
var criteria = searcher.CreateQuery("content");
var filter = criteria.Field("nodeTypeAlias", "CWS_Home".Boost(20));
Proximity
var searcher = indexer.GetSearcher();
//Arrange
var criteria = searcher.CreateQuery("content");
//get all nodes that contain the words warren and creative within 5 words of each other
var filter = criteria.Field("metaKeywords", "Warren creative".Proximity(5));
Fuzzy
var searcher = indexer.GetSearcher();
var criteria = searcher.CreateQuery();
var filter = criteria.Field("Content", "think".Fuzzy(0.1F));
Escape
var exactcriteria = searcher.CreateQuery("content");
var exactfilter = exactcriteria.Field("__Path", "-1,123,456,789".Escape());
var results2 = exactfilter.Execute();