Session: Querying: How to Perform a Faceted Search
To execute a facet query using the session Query
method, use ToFacets
extension. There is also a possibility to convert the query straight into the FacetQuery
instance using the ToFacetQuery
extension.
Syntax
IAggregationQuery<T> AggregateBy<T>(FacetBase facet);
IAggregationQuery<T> AggregateBy<T>(IEnumerable<FacetBase> facets);
IAggregationQuery<T> AggregateBy<T>(Action<IFacetBuilder<T>> builder);
IAggregationQuery<T> AggregateUsing<T>(string facetSetupDocumentKey);
Parameters | ||
---|---|---|
facets | List<Facet> | List of facets required to perform a facet query (mutually exclusive with facetSetupDoc ). |
facetSetupDoc | string | Document key that contains predefined FacetSetup (mutually exclusive with facets ). |
start | int | number of results that should be skipped. Default: 0 . |
pageSize | int | maximum number of results that will be retrieved. Default: null . |
Return Value | |
---|---|
FacetResults | Facet query results with query Duration and list of Results : One entry for each term/range as specified in [FacetSetup] document or passed in parameters. |
Example I
_1
Dictionary<string, FacetResult> facets = session
.Query<Camera>("Camera/Costs")
.AggregateBy(new Facet
{
FieldName = "Manufacturer",
Options = new FacetOptions
{
TermSortMode = FacetTermSortMode.CountDesc
}
})
.AndAggregateBy(new RangeFacet<Camera>
{
Ranges =
{
camera => camera.Cost < 200m,
camera => camera.Cost >= 200m && camera.Cost < 400m,
camera => camera.Cost >= 400m && camera.Cost < 600m,
camera => camera.Cost >= 600m && camera.Cost < 800m,
camera => camera.Cost >= 800m
},
Aggregations =
{
{
FacetAggregation.Average,
new HashSet<FacetAggregationField> { new FacetAggregationField { Name = "Cost" } }
}
}
})
.AndAggregateBy(new RangeFacet<Camera>
{
Ranges =
{
camera => camera.Megapixels < 3.0,
camera => camera.Megapixels >= 3.0 && camera.Megapixels < 7.0,
camera => camera.Megapixels >= 7.0 && camera.Megapixels < 10.0,
camera => camera.Megapixels >= 10.0
}
})
.Execute();
Example II
_1
Dictionary<string, FacetResult> facets = session
.Query<Camera>("Camera/Costs")
.AggregateBy(builder => builder
.ByField(x => x.Manufacturer)
.WithOptions(new FacetOptions
{
TermSortMode = FacetTermSortMode.CountDesc
}))
.AndAggregateBy(builder => builder
.ByRanges(
camera => camera.Cost < 200m,
camera => camera.Cost >= 200m && camera.Cost < 400m,
camera => camera.Cost >= 400m && camera.Cost < 600m,
camera => camera.Cost >= 600m && camera.Cost < 800m,
camera => camera.Cost >= 800m)
.AverageOn(x => x.Cost))
.AndAggregateBy(builder => builder
.ByRanges(
camera => camera.Megapixels < 3.0,
camera => camera.Megapixels >= 3.0 && camera.Megapixels < 7.0,
camera => camera.Megapixels >= 7.0 && camera.Megapixels < 10.0,
camera => camera.Megapixels >= 10.0))
.Execute();
Converting Query into FacetQuery
_1
session.Store(new FacetSetup
{
Facets = new List<Facet>
{
new Facet
{
FieldName = "Manufacturer"
}
},
RangeFacets = new List<RangeFacet>
{
new RangeFacet<Camera>
{
Ranges =
{
camera => camera.Cost < 200m,
camera => camera.Cost >= 200m && camera.Cost < 400m,
camera => camera.Cost >= 400m && camera.Cost < 600m,
camera => camera.Cost >= 600m && camera.Cost < 800m,
camera => camera.Cost >= 800m
}
},
new RangeFacet<Camera>
{
Ranges =
{
camera => camera.Megapixels < 3.0,
camera => camera.Megapixels >= 3.0 && camera.Megapixels < 7.0,
camera => camera.Megapixels >= 7.0 && camera.Megapixels < 10.0,
camera => camera.Megapixels >= 10.0
}
}
}
}, "facets/CameraFacets");
session.SaveChanges();
Dictionary<string, FacetResult> facets = session
.Query<Camera>("Camera/Costs")
.AggregateUsing("facets/CameraFacets")
.Execute();
Parameters | ||
---|---|---|
facets | List<Facet> | List of facets required to perform a facet query (mutually exclusive with facetSetupDoc ). |
facetSetupDoc | string | Document key that contains predefined FacetSetup (mutually exclusive with facets ). |
start | int | number of results that should be skipped. Default: 0 . |
pageSize | int | maximum number of results that will be retrieved. Default: null . |
Return Value | |
---|---|
FacetQuery | Instance of FacetQuery containing all options set in Query . Can be used with MultiFacetedSearch from Advanced session operations or with Commands directly. |
Example
_1
Dictionary<string, FacetResult> facets = session
.Query<Camera>("Camera/Costs")
.AggregateBy(builder => builder
.ByField(x => x.Manufacturer)
.MinOn(x => x.Cost)
.MaxOn(x => x.Megapixels)
.MaxOn(x => x.Zoom))
.AndAggregateBy(builder => builder
.ByRanges(
camera => camera.Cost < 400m,
camera => camera.Cost >= 400m)
.AverageOn(x => x.Cost)
.MaxOn(x => x.Cost)
.MinOn(x => x.Cost))
.Execute();