NEST. Repository
A simple encapsulation with NEST Client for search data form Elasticsearch.
GitHub
Apinestreaderrepository
TEntityGet (TKeyID);TEntityGet (func<querycontainerdescriptor<Tentity>,Querycontainer>Filterexp=Nullfunc<sourcefilterdescriptor<Tentity>,Isourcefilter>Includefieldexp=Nullexpression<func<TEntity,Object>>Sortexp=NullSortOrderSortType= SortOrder.Ascending);tuple<Long, list<tentity>>GetList (func<querycontainerdescriptor<Tentity>,Querycontainer>filterexp = null, Func<sourcefilterdescriptor<TENTITY> ISourceFilter> Span class= "Pl-smi" >includefieldexp = null, Expression <func<tentity, object>> sortexp = null, SortOrder sorttype = sortorder.ascending, int Limit = 10, int skip = 0)
Nestreaderrepositoryasync
task<Tentity>Getasync (TKeyID);task<Tentity>Getasync (func<querycontainerdescriptor<Tentity>,Querycontainer>Filterexp=Nullfunc<sourcefilterdescriptor<Tentity>,Isourcefilter>Includefieldexp=Nullexpression<func<TEntity,Object>>Sortexp=NullSortOrderSortType= SortOrder.Ascending);task<tuple<Long, list<tentity>>>Getlistasync (func<querycontainerdescriptor<Tentity>,Querycontainer>filterexp = null, Func<sourcefilterdescriptor<TENTITY> ISourceFilter> Span class= "Pl-smi" >includefieldexp = null, Expression <func<tentity, object>> sortexp = null, SortOrder sorttype = sortorder.ascending, int Limit = 0, int skip = 0)
Depend on
NEST 6.0.2repository.ientity 2.0. 1 (Youcan write ientity<t> interface and you entity inherit it.)
How to use
First, you need has an entity inherit Ientity<t>, and T is type of PrimaryKey. eg
[Serializable][bsonignoreextraelements]public class user: ientity<long>{[Bsonid] public long id {get; set;} public double age {get; set;} public double sex {get; set;} public string like {get; set; }}
Second, you need has a repository inherit nestreaderrepository or Nestreaderrepositoryasync. eg
nestreaderrepository<"Http://localhost:9200/Base (connstring) {}}
Now, you can search the data with the simple API. eg
Staticvoidmain (string[] args) { Repository.container.repositorycontainer.register<userrepo> (); var userrepo = repository.container.repositorycontainer.resolve< Userrepo> (); var user = Userrepo.get (9); var users = userrepo.getlist (filterexp:q = +q.range (r = R.Field (f = F.age). GreaterThan (13). LessThan (28), includefieldexp:p = p.includes (i = i.fields (f = f.f = F.sex, f = f.like), sortexp:s = S.age , sortType:Nest.SortOrder.Ascending, limit: 100, skip: 0);}
How to write a query0x00. Structured Search
By default, documents would be returned in _score descending order, where the _score for each hits is the relevancy score CA Lculated for why well the document matched the query criteria.
Q = Q.daterange (r = r) . Field (f = F.{field with DateTime Type}) . Greaterthanorequals (DateTime (01)). LessThan (DateTime ))
The benefit of executing a query in a filter context was that Elasticsearch was able to forgo calculating a relevancy score, As well as the cache filters for faster subsequent performance.
Q = q.bool (b = b.filter (BF = BF) . DateRange (r = r) . Field (f = F.{field with DateTime Type}) . Greaterthanorequals (DateTime (01)). LessThan (() ))))
0x01. Unstructured Search
Full text queries (find all documents, contain "Russ" in the lead developer First Name field)
Q = q.match (M + = m) . Field (f = f.leaddeveloper.firstname) . Query ("Russ"))
0x02. Combining Search
Q = Q.bool (b + = b . Must (mu = mu . Match (m = M . Field (f = f.leaddeveloper.firstname) . Query ("Russ") ), mu = mu . Match (m = M . Field (f = f.leaddeveloper.lastname) . Query ("Cam")). Filter (fi = fi. DateRange (r = r). Field (f = f.startedon). Greaterthanorequals (DateTime (01)). LessThan (() ))))
Use operator
Q = q.match (M + = m) . Field (f = f.leaddeveloper.firstname) . Query ("Russ") ) && Q . Match (m = M . Field (f = f.leaddeveloper.lastname) . Query ("Cam")) && +q. DateRange (r = r). Field (f = f.startedon). Greaterthanorequals (DateTime (01)). LessThan (DateTime )))
Should ==> OR ==> | |
Must ==> and ==> &&
Must_not ==> not==>!
Filter ==> +
The query would be converted to a BOOL query if use any operator, and the answer to the BOOL query are always yes or no, so That would not be score.
Reference
Https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-queries.html
Https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html
Simple encapsulation based on Netcore for elasitsearch client nest query function nest.repository