gpt4 book ai didi

c# - 如何在.net nest中更新现有已过滤的Elasticsearch查询

转载 作者:行者123 更新时间:2023-12-03 00:19:37 25 4
gpt4 key购买 nike

我有一个来自gui的json查询。它已经过滤了查询部分。我想使用日期范围过滤器扩展过滤的查询。如何使用嵌套。我尝试了以下方法(QueryTest方法),但失败了。

public SearchDescriptor<dynamic> GetSearchDescriptor(string queryJson = null)
{
var searchDesc = new SearchDescriptor<dynamic>();

if (string.IsNullOrEmpty(queryJson))
{
return searchDesc;
}

searchDesc = _nestClient.Serializer.Deserialize<SearchDescriptor<dynamic>>(new MemoryStream(Encoding.UTF8.GetBytes(queryJson)));

return searchDesc;
}

public string ConvertToJsonQuery(SearchDescriptor<dynamic> searchDescriptor)
{
return Encoding.UTF8.GetString(_nestClient.Serializer.Serialize(searchDescriptor, SerializationFormatting.Indented));
}

public void QueryTest()
{
var query = @"{
""query"": {
""filtered"": {
""filter"": {
""bool"": {
""must"": [
{
""term"": {
""display_name"": ""NBLEYLEK$""
}
}
]
}
}
}
}
}";

var searchDescriptor = GetSearchDescriptor(query).AllTypes();

var queryLastA = ConvertToJsonQuery(searchDescriptor);

searchDescriptor.Query(x => x.Filtered(y => y.Filter(z => z.Range(t => t.OnField("sign_time").Greater(DateTime.Now)))));

var queryLastB = ConvertToJsonQuery(searchDescriptor);
}

如果查看queryLastA和queryLastB的值,我将看到:

queryLastA =>
{      
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"display_name": "NBLEYLEK$"
}
}
]
}
}
}
}
}

queryLastB =>
{
"query": {
"filtered": {
"filter": {
"range": {
"sign_time": {
"gt": "2015-08-25T16:13:56.694"
}
}
}
}
}
}

所需查询为:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"display_name": "NBLEYLEK$"
}
},
{
"range": {
"sign_time": {
"gt": "2015-08-25T16:13:56.694"
}
}
}
]
}
}
}
}
}

我期望从嵌套到旧过滤器下追加新过滤器,但它会忽略旧过滤器(将queryLastB与queryLastA比较)。有什么建议吗?

最佳答案

当前在嵌套中,如果我们提供新的过滤器,它将覆盖旧的过滤器。我有同样的问题。我对查询进行了字符串操作,并将其转换为C#类,并使用nest从头开始应用了过滤器。这为我解决了。

关于c# - 如何在.net nest中更新现有已过滤的Elasticsearch查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205213/

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