gpt4 book ai didi

c# - 带有对象初始化器 NEST 5.x 的 Elasticsearch 嵌套动态查询

转载 作者:行者123 更新时间:2023-11-30 16:43:19 25 4
gpt4 key购买 nike

大家好,我是 elastic nest API 的新手,我使用的是 nest 5.x。我目前正在开发某种高级搜索页面,因此当用户不检查条件时,我不必在查询中包含该过滤器。我正在尝试将 must 运算符下的 2 个查询与使用 nest 的对象初始化方法结合起来。如何实现?我正在关注 [ 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 - Building Dynamic Nested Query

这是我的代码

    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;
}
}

但这会返回零结果。如何做到这一点?无论如何谢谢。

编辑:

这是参数变量定义。其搜索关键词的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);
}
}
}

最佳答案

But it doesnt work cause Field class doesnt accept type arguments.

您需要确保包含 using static directive对于 Nest.Infer

using static Nest.Infer;

与其余的 using 指令。

.Query(q => q.Bool(qb => qb.Must(m => m.MatchAll() && andQuery))));

不需要包裹在Must()中,直接做

.Query(q => q.MatchAll() && andQuery)

这会将两个查询包装在 bool 查询 must 子句中。您也不需要检查 nullandQuery,因为 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 有 conditionless queries 的概念因此您无需检查 queries.queryfields 是否为 null 或为空,只需构建查询并将其添加到 andQuery。这样就变成了

var val = queries.queryfields;

andQuery &= new TermQuery
{
Field = queries.queryfields,
Value = val
};

一边

所有 NEST 文档都是从源代码生成的;您可以通过单击文档中的任何编辑 链接来追溯到原始源文件。这将带您到 github 页面,such as this one for bool queries .从这里开始,该文档包含一个重要说明,该说明链接回 original source .

关于c# - 带有对象初始化器 NEST 5.x 的 Elasticsearch 嵌套动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com