c# - How do I accurately represent this ElasticSearch query using NEST? -
background / goal
i have query in elasticsearch i'm using filters on several fields (relatively small data set , know values should in fields @ time query). idea we'll perform full-text query after we've filtered on selections made user.
i'm putting elasticsearch behind webapi controller , figured made sense use nest accomplish query.
the query, in plain english
we have filters several fields. each inner filter or filter, they're and.
in sql, pseudo-code equivalent select * table foo in (1,2,3) , bar in (4,5,6)
.
questions
- can simplify way i'm thinking query, based on see below? overlooking basic approach? seems heavy i'm new es.
- how represent query below in nest syntax?
- is nest best choice this? should using elasticsearch library instead , going lower level?
the query text
{ "query": { "filtered": { "filter": { "bool": { "must": [ { "or": [ { "term": { "foo": "something" } }, { "term": { "foo": "somethingelse" } } ] }, { "or": [ { "term": { "bar": "something" } }, { "term": { "bar": "somethingelse" } } ] } ] } } } }, "size": 100 }
this kind of task quite simple , popular in es. can represent in nest following:
var rs = es.search<dynamic>(s => s .index("your_index").type("your_type") .from(0).size(100) .query(q => q .filtered(fq => fq .filter(ff => ff .bool(b => b .must( m1 => m1.terms("foo", new string[] { "something", "somethingelse" }), m2 => m2.terms("bar", new string[] { "something", "somethingelse" }) ) ) ) .query(qq => qq .matchall() ) ) ) );
some notes:
- i use filtered query filter need first, search stuffs later. in case filter
foo in ("something", "somethingelse") , bar in ("something", "somethingelse")
, query filtered results (match_all
). can changematch_all
need.filtered query
it's best performance es need evaluate scores of documents inquery
part (after filtered), not documents. - i use
terms
filter, more simple , better performanceor
. default mode ofterms
or input terms, can refer more in document available modes (and, or, plain, ...).
nest best choice .net in opinion designed simple & easy use purposes. used lower api if want use new features nest not support @ time, or if nest have bugs in functions use. can refer here brief nest tutorial: http://nest.azurewebsites.net/nest/writing-queries.html
updated: building bool filters dynamic:
var filters = new list<nest.filtercontainer>(); filters.add(nest.filter<dynamic>.terms("foo", new string[] { "something", "somethingelse" })); // ... more filter
then replace .bool(b => b.must(...))
.bool(b => b.must(filters.toarray()))
hope help
Comments
Post a Comment