问题
I have upgraded my elasticsearch from v1.9 to v5 and I have noticed that a lot of things have changed.
If I were to take an example from v1.9: The below code checks whether the object type matches ObjectAdo and it filters the items within ObjectAdo that have the IsDeleted field as false.
private Func<FilterDescriptor<dynamic>, FilterContainer> Filter()
{
return b => b.Bool(x => x.Must(m => m.Type(typeof(ObjectAdo)), n => n.Term("IsDeleted", false)));
}
Now, after upgrading from v1.9 to v5, I noticed that FilterDescriptor got changed to QueryContainerDescriptor and FilterContainer is now QueryContainer. Now I am unable to perform the same operation as before because .Type(typeof(ObjectAdo) is no longer available.
So I am wondering if there is an alternative solution to check the object type.
https://i.stack.imgur.com/dOPex.png
回答1:
In Elasticsearch 2.0, queries and filters merged into one, with the notion of a query context and a filter context; when wrapped in a bool query filter clause, a query/filter is in a filter context so relevance scores will not be calculated and it will be cacheable.
NEST 2.x onwards aligns with the change in Elasticsearch 2.0 and has queries (QueryContainer, QueryContainerDescriptor<T>, etc.) that can be used in both query and filter contexts.
Applying this to your filter, would mean changing it to the following with NEST 5.x
private Func<QueryContainerDescriptor<dynamic>, QueryContainer> Filter()
{
return b => b.Bool(x => x
.Filter(
f => f.Type(t => t.Value(typeof(ObjectAdo))),
f => f.Term("IsDeleted", false)
)
);
}
The syntax for a type query has changed a little to take a lambda expression to set available options.
来源:https://stackoverflow.com/questions/44469558/elasticsearch-filtering-the-type