嗨我'm a new to elastic nest API and I'米使用嵌套5.x.我检查了一个标准,我试图在使用nest的对象初始化方法下将必须运算符下的2个查询组合起来 . [14448522_]怎么实现呢?我正在关注[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),值=“y”}});
但它不起作用,因为Field类不接受类型参数 .
我也尝试从这个主题中采用这种方法[Nest Elastic - Building Dynamic Nested Query
这是我的代码
public HttpResponseMessage GetSearchResult([FromUri] SearchModels查询)
{
尝试
{
///
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和Query = null;
// List <QueryContainer> QueryContainers = new List <QueryContainer>();
IDXNetAnnouncement record =新IDXNetAnnouncement
;
#region关键字
if(!string.IsNullOrEmpty(queries.queryfields))
{
var val = queries.queryfields;
TermQuery tq = new TermQuery
{
Field = queries.queryfields,
值= val
};
if(andQuery == null)
andQuery = tq;
其他
andQuery&= tq;
//QueryContainers.Add(tq);
}
#endregion关键字
#region kodeemiten
if(!string.IsNullOrEmpty(queries.kodeemiten))
{
var val = queries.kodeemiten;
TermQuery tq = new TermQuery
{
Name =“kode_emiten”,
Field = record.kode_emiten,
值= val
};
if(andQuery == null)
andQuery = tq;
其他
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;
其他
andQuery&= dq;
//QueryContainers.Add(dq);
}
#endregion关键字
var reqs =(ISearchResponse <IDXNetAnnouncement>)null;
if(andQuery!= null)
{
reqs = conn.client.Search <IDXNetAnnouncement>(s => s
.AllIndices()
. 所有类型()
. 从(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);
}
其他
{
reqs = conn.client.Search <IDXNetAnnouncement>(s => s
.AllIndices()
. 所有类型()
. 从(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”);
返回;
}
catch(例外e)
{
var resp = new HttpResponseMessage()
{
Content = new StringContent(e.ToString())
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue(“application / json”);
返回;
}
}
但是返回零结果 . 怎么做到这一点?无论如何,谢谢 .
编辑:
这是params变量定义 . 它的搜索关键字的apoco模型
公共类SearchModels
{
public string queryfields {get;组; }
public string datefrom {get;组; }
public string dateto {get;组; }
public string emitentype {get;组; }
public string kodeemiten {get;组; }
public string issuercode {get;组; }
public int indexfrom {get;组; }
public int pagesize {get;组; }
}
IDXNetAnnouncement是搜索结果的poco模型 . 它实际上是一种存储在弹性服务器上的文档类型
公共类IDXNetAnnouncement
{
public string perihalpengumuman {get;组; }
公共字符串附件{get;组; }
public string createddate {get;组; }
public bool efekemiten_spei {get;组; }
public string jmsxgroupid {get;组; }
public string tglpengumuman {get;组; }
public object errordescription {get;组; }
public string ESversion {get;组; }
public int oldfinalid {get;组; }
public bool efekemiten_etf {get;组; }
公共对象errorcode {get;组;}
public string jenisemiten {get;组; }
public int pkid {get;组; }
public string judulpengumuman {get;组; }
public string form_id {get;组; }
public bool efekemiten_eba {get;组; }
public string jenispengumuman {get;组; }
public string nopengumuman {get;组; }
public string kode_emiten {get;组; }
public string divisi {get;组; }
public string EStimestamp {get;组; }
public bool efekemiten_obligasi {get;组; }
public long finalid {get;组; }
public bool efekemiten_saham {get;组; }
public string kodedivisi {get;组; }
公共字符串SearchTerms
{
得到
{
return string.Format(“{0} {1} {2}”,judulpengumuman,kode_emiten,nopengumuman);
}
}
}
1 回答
您需要确保为
Nest.Infer
包含using static directive,即与其余的using指令一起使用 .
无需包裹
Must()
,只需这样做它将在
bool
查询must
子句中包装两个查询 . 您也不需要null
检查andQuery
,因为NEST足够智能,如果其中一个或两个都是null
,则不能合并这两个查询 .TermQuery tq = new TermQuery
{
Field = queries.queryfields,
值= val
};
if(andQuery == null)
andQuery = tq;
其他
andQuery&= tq;
//QueryContainers.Add(tq);
}
NEST具有conditionless queries的概念,因此您无需检查
queries.queryfields
是空还是空,只需构建查询并将其添加到andQuery
. 所以它会成为旁边
所有NEST文档都是从源代码生成的;您可以通过单击文档中的任何 edit 链接追溯回原始源文件 . 这将带你到一个github页面,such as this one for bool queries . 从这里开始,该文档包含一个重要的注释,链接回original source .