gpt4 book ai didi

c# - 在Elasticsearch 5中按聚合结果过滤

转载 作者:行者123 更新时间:2023-12-03 01:48:51 25 4
gpt4 key购买 nike

我正在使用ES5。在C#上使用Nest lib。

我的模型中有两个实体,即Contact和events。我有一个要求,我必须获得所有联系,而不是拥有超过N个事件(非常类似于对SQL进行查询)。联系人是 Activity 的 parent ,因此我可以使用 parent /子女策略进行过滤。

我无法通过这些聚集来过滤联系人,因此可以获取联系人的汇总。

我做了这样的事情:

var queryResult = client.Search<Contact>(s => s
.Index("contact*")
.Query(q => q
...
)
.Aggregations(a => a
.Children<Event>("filter_event", ca => ca.Aggregations(ca2 => ca2
.Filter("filter1", f => f.Filter(fq => fq.Term(t => t.Field(tf => tf.EventName).Value("Event1")))
.Aggregations(fa => fa
.Terms("filter1Contacts", v => v.Field(faf => faf.EventContactGuid).Size(int.MaxValue).MinimumDocumentCount(5))
)
)
)))
);

使用此代码,我只能获得具有5个以上事件的那些联系人的汇总,但是我没有找到一种基于这些汇总结果筛选联系人的方法。

ES 5中有办法做到这一点吗?

最佳答案

您为此使用 has_child query,这是在Linqpad中尝试的示例

void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "orders";
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex)
.InferMappingFor<Order>(m => m
.IdProperty(f => f.Customer)
)
.PrettyJson()
.DisableDirectStreaming()
.OnRequestCompleted(response =>
{
// log out the request
if (response.RequestBodyInBytes != null)
{
Console.WriteLine(
$"{response.HttpMethod} {response.Uri} \n" +
$"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
}
else
{
Console.WriteLine($"{response.HttpMethod} {response.Uri}");
}

Console.WriteLine();

// log out the response
if (response.ResponseBodyInBytes != null)
{
Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
$"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
$"{new string('-', 30)}\n");
}
else
{
Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
$"{new string('-', 30)}\n");
}
});

var client = new ElasticClient(connectionSettings);

if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);

client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<Order>(mm => mm.AutoMap())
.Map<OrderLine>(mm => mm
.Parent<Order>()
.AutoMap()
)
)
);

var orders = new[]
{
new Order { Customer = "Bilbo Baggins" },
new Order { Customer = "Gandalf the Grey" }
};

var orderlines = new Dictionary<string, OrderLine[]>
{
{ "Bilbo Baggins",
new []
{
new OrderLine { ItemNumber = 1 },
new OrderLine { ItemNumber = 2 },
new OrderLine { ItemNumber = 3 },
new OrderLine { ItemNumber = 4 },
new OrderLine { ItemNumber = 5 }
}
},
{ "Gandalf the Grey",
new []
{
new OrderLine { ItemNumber = 1 },
new OrderLine { ItemNumber = 2 },
new OrderLine { ItemNumber = 3 },
new OrderLine { ItemNumber = 4 }
}
}
};

client.IndexMany(orders);

foreach (var lines in orderlines)
{
client.Bulk(b => b
.IndexMany(lines.Value, (bi, d) => bi.Parent(lines.Key))
);
}

client.Refresh(defaultIndex);

var queryResult = client.Search<Order>(s => s
.Query(q => +q
.HasChild<OrderLine>(c => c
.Query(cq => +cq.MatchAll())
// min number of child documents that must match
.MinChildren(5)
)
)
);
}

public class Order
{
public string Customer { get; set; }
}

public class OrderLine
{
public int ItemNumber { get; set; }
}

查询结果仅返回Bilbo Baggins
Status: 200
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [ {
"_index" : "orders",
"_type" : "order",
"_id" : "Bilbo Baggins",
"_score" : 0.0,
"_source" : {
"customer" : "Bilbo Baggins"
}
} ]
}
}

关于c# - 在Elasticsearch 5中按聚合结果过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009990/

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