Nest.net Client for Elasticsearch simple application

Source: Internet
Author: User

As a result of a recent project in the search section to use Elasticsearch to achieve the search function, suffering from poor English and this aspect of systematic information is not easy to find, in the implementation of a lot of problems encountered, now the whole process of code sharing, to the same groping people some reference,  At the same time, I hope that experienced Daniel found that there is a problem where the treatise! Query the entry WEBAPI part of the code, the query conditions encapsulated in ejobqueryposition var param = new Ejobqueryposition
{
Industrypost = Industrypost,
region = region,
Minsalary = Minsalary,
};//Call Search var gl = await _querybusiness.querypositionsasync (param, Skip, top, String. Empty); Processing results if (GL. IsError)
Return Failactionrequest (Gl.webapi_error);
if (gl.total <= 0)
{
Return Okactionrequest (GL);
}

Return Oklistactionrequest (GL); Querypositionsasync part code var resultdata = await _querypostion.queryasync (condition, skip, top, searchtext);

View Search Statements
var temp= Encoding.UTF8.GetString (resultData.RequestInformation.Request). Trim ();

if (!resultdata.hits.any ())
{
return new glistresult<ejobpositionsimple> (new ejobpositionsimple[0]);
}
var result = resultdata.documents;

var G1 = result. Select (position = new Ejobpositionsimple
{
pstn_id = position._id,
...
Phone = position. Phone,
Name = position. Brandname,
}). ToList ();

return new Glistresult<ejobpositionsimple> (G1, (int) resultdata.total); Queryasync part code///<summary>
Build the Search Client
</summary>
<returns></returns>
private static Elasticclient getsearchclient ()
{
var connectstring = configurationmanager.connectionstrings["ElasticSearch"]. ConnectionString;
var nodesstr = connectstring.split (' | ');
var nodes = Nodesstr.select (s = = new Uri (s)). ToList ();
var connectionpool = new Sniffingconnectionpool (nodes);
var settings = new ConnectionSettings (ConnectionPool). Setdefaultindex ("position");
Settings. Setdefaultpropertynameinferrer (P = p.tostring ());
var client = new Elasticclient (settings);
return client;
}//web.config ElasticSearch settings <add name= "ElasticSearch" connectionstring= "http://192.168.1.7:7200"/>//default is 9 200 ports
Public async task<isearchresponse<qposition>> Queryasync (ejobqueryposition condition, int skip, int top, String searchtext = "")
{
var client = Getsearchclient ();

var resultdata = await client. Searchasync<qposition> (s = =
S.index ("position"). Type ("Tbjobposition").
Query (
Q = querycontainerposition (condition, SearchText, q)).
SortDescending (f = f.updatetime). Skip (Skip). Take (top));
return resultdata;
}//<summary>
Generate position Query expression
</summary>
<param name= "condition" > Job conditions </param>
<param name= "SearchText" > Search keywords </param>
<param name= "Q" > Query expression for return </param>
<returns> returns an expression for search </returns>
private static Querycontainer querycontainerposition (ejobqueryposition condition, string searchtext, Querydescriptor <QPosition> q)
{
var query = new Querycontainer ();
Not expired
Query &= q.range (rr = = RR. Onfield (FF = ff). Expiretime). Greaterorequals (DateTime.UtcNow.Date));

if (!string. Isnullorwhitespace (SearchText))
{
Query &= querytext (searchtext, q, query);
}
return query; This section supplements the instructions here to construct multiple search conditions as follows */var query1 = new Querycontainer ();
Query1 = Q.term (s = = S.onfield (ff = = FF). brandname). Value ("szbrandname1"));

var query2 = new Querycontainer ();
Query2 = Q.term (s = = S.onfield (ff = = FF). brandname). Value ("szbrandname2"));

var query3 = new Querycontainer ();
Query2 = Q.term (s = = S.onfield (ff = = FF). brandname). Value ("Szbrandname3"));
query = Query1 | | Query2 | |            Query3;            or query = (Query1 | | query2) && query3; Description of the conditions | | Equivalent to the should in query; && equivalent must//partial search conditions can use filter performance better, slightly */
}//<summary>
Search for text expressions, which must be placed at the end of all expressions
</summary>
<param name= "SearchText" > Search text </param>
<param name= "q" > Search Expressions </param>
<param name= "Query" > the search Expression class to return </param>
<returns></returns>
private static Querycontainer QueryText (string searchtext, querydescriptor<qposition> Q, querycontainer query)
{
if (!string. Isnullorwhitespace (SearchText))
{
query = q.matchphrase (p = = P.onfield (o = o.descript). Query (SearchText));
}
return query;
The above code is placed in the corresponding CS file can be implemented postion search function part of the application to add oh using System;
Using System.Configuration;
Using System.Linq;
Using System.Threading.Tasks;
Using * *. Entity;
Using Elasticsearch.Net.ConnectionPool;
using Nest; Reference URL http://nest.azurewebsites.net/nest/core/

(GO) nest.net Client for elasticsearch simple application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.