[英]Elastic search nest dynamic query with object initializer NEST 5.x
嗨,我是彈性嵌套API的新手,我正在使用嵌套5.x. 我目前正在開發某種高級搜索頁面,因此當用戶不檢查條件時,我不必在我的查詢中包含該過濾器。 我正在嘗試在使用nest的對象初始化方法下將must運算符下的2個查詢組合起來。 怎么實現呢? 我正在關注[ https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html]上的示例
var secondSearchResponse = client.Search(new SearchRequest {Query = new TermQuery {Field = Field(p => p.Name),Value =“x”} && new TermQuery {Field = Field(p => p.Name),Value =“y”}});
但它不起作用,因為Field類不接受類型參數。
我也嘗試從這個主題[ Nest Elastic - 構建動態嵌套查詢]中遵循這種方法
這是我的代碼
public HttpResponseMessage GetSearchResult([FromUri] SearchModels queries) { try { /// string result = string.Empty; result += "queryfields + " + queries.queryfields == null ? string.Empty : queries.queryfields; result += "datefrom + " + queries.datefrom == null ? string.Empty : queries.datefrom; result += "dateto + " + queries.dateto == null ? string.Empty : queries.dateto; result += "emitentype + " + queries.emitentype == null ? string.Empty : queries.emitentype; QueryContainer andQuery = null; //List<QueryContainer> QueryContainers = new List<QueryContainer>(); IDXNetAnnouncement record = new IDXNetAnnouncement { kode_emiten = queries.kodeemiten }; #region keyword if (!string.IsNullOrEmpty(queries.queryfields)) { var val = queries.queryfields; TermQuery tq = new TermQuery { Field = queries.queryfields, Value = val }; if (andQuery == null) andQuery = tq; else andQuery &= tq; //QueryContainers.Add(tq); } #endregion keyword #region kodeemiten if (!string.IsNullOrEmpty(queries.kodeemiten)) { var val = queries.kodeemiten; TermQuery tq = new TermQuery { Name = "kode_emiten", Field = record.kode_emiten, Value = val }; if (andQuery == null) andQuery = tq; else andQuery &= tq; //QueryContainers.Add(tq); } #endregion #region date if (!string.IsNullOrEmpty(queries.datefrom) && !string.IsNullOrEmpty(queries.dateto)) { DateRangeQuery dq = new DateRangeQuery(); dq.Name = "tglpengumuman"; dq.LessThanOrEqualTo = DateMath.Anchored(queries.dateto); dq.GreaterThanOrEqualTo = DateMath.Anchored(queries.datefrom); dq.Format = "dd/mm/yyyy"; if (andQuery == null) andQuery = dq; else andQuery &= dq; //QueryContainers.Add(dq); } #endregion keyword var reqs = (ISearchResponse<IDXNetAnnouncement>)null; if (andQuery != null) { reqs = conn.client.Search<IDXNetAnnouncement>(s => s .AllIndices() .AllTypes() .From(queries.indexfrom) .Size(queries.pagesize) .Query(q => q.Bool(qb => qb.Must(m => m.MatchAll() && andQuery)))); //var json = conn.client.Serializer.SerializeToString(reqs.ApiCall.ResponseBodyInBytes); } else { reqs = conn.client.Search<IDXNetAnnouncement>(s => s .AllIndices() .AllTypes() .From(queries.indexfrom) .Size(queries.pagesize) .Query(m => m.MatchAll())); } //var reqstring = Encoding.UTF8.GetString(conn.client.); var reslts = this.conn.client.Serializer.SerializeToString(reqs,SerializationFormatting.Indented); var resp = new HttpResponseMessage() { Content = new StringContent(reslts) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return resp; } catch (Exception e) { var resp = new HttpResponseMessage() { Content = new StringContent(e.ToString()) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return resp; } }
但是返回零結果。 怎么做到這一點? 無論如何,謝謝。
編輯:
這是params變量定義。 它的搜索關鍵字的apoco模型
public class SearchModels { public string queryfields { get; set; } public string datefrom { get; set; } public string dateto { get; set; } public string emitentype { get; set; } public string kodeemiten { get; set; } public string issuercode { get; set; } public int indexfrom { get; set; } public int pagesize { get; set; } }
IDXNetAnnouncement是搜索結果的poco模型。 它實際上是一種存儲在彈性服務器上的文檔類型
public class IDXNetAnnouncement { public string perihalpengumuman { get; set; } public string attachments { get; set; } public string createddate { get; set; } public bool efekemiten_spei { get; set; } public string jmsxgroupid { get; set; } public string tglpengumuman { get; set; } public object errordescription { get; set; } public string ESversion { get; set; } public int oldfinalid { get; set; } public bool efekemiten_etf { get; set; } public object errorcode { get; set; } public string jenisemiten { get; set; } public int pkid { get; set; } public string judulpengumuman { get; set; } public string form_id { get; set; } public bool efekemiten_eba { get; set; } public string jenispengumuman { get; set; } public string nopengumuman { get; set; } public string kode_emiten { get; set; } public string divisi { get; set; } public string EStimestamp { get; set; } public bool efekemiten_obligasi { get; set; } public long finalid { get; set; } public bool efekemiten_saham { get; set; } public string kodedivisi { get; set; } public string SearchTerms { get { return string.Format("{0} {1} {2}", judulpengumuman, kode_emiten, nopengumuman); } } }
但它不起作用,因為Field類不接受類型參數。
您需要確保為Nest.Infer
包含一個using static指令
using static Nest.Infer;
與其余的using指令一起使用。
.Query(q => q.Bool(qb => qb.Must(m => m.MatchAll()&& andQuery))));
不需要包裝Must()
,只需要
.Query(q => q.MatchAll() && andQuery)
這將在bool
查詢must
子句中包裝兩個查詢。 您也不需要null
check andQuery
因為NEST足夠聰明,如果其中一個或兩個都為null
則不能組合這兩個查詢。
if (!string.IsNullOrEmpty(queries.queryfields)) { var val = queries.queryfields; TermQuery tq = new TermQuery { Field = queries.queryfields, Value = val }; if (andQuery == null) andQuery = tq; else andQuery &= tq; //QueryContainers.Add(tq); }
NEST具有無條件查詢的概念,因此您無需檢查queries.queryfields
為null或為空,只需構建查詢並將其添加到andQuery
。 所以它會成為
var val = queries.queryfields;
andQuery &= new TermQuery
{
Field = queries.queryfields,
Value = val
};
所有NEST文檔都是從源代碼生成的; 您可以通過單擊文檔中的任何編輯鏈接追溯到原始源文件。 這將帶您進入github頁面, 例如這個用於bool查詢的頁面 。 從這里開始,該文檔包含一個重要的注釋,鏈接回原始源 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.