0

使用 ElasticSearch 7.6 我想找到区域列表中的所有点。还有几个其他过滤条件被添加到 QueryContainerDescriptors 列表中并应用于最终搜索。

 var queries = new List<Func<QueryContainerDescriptor<MySearchableObject>, QueryContainer>>();

此示例适用于一个区域,其中 Coordinates 是字典

foreach (var area in areas)
{
    var pointsList = area.Coordinates.Values.Select(p => new GeoLocation(p.Latitude, p.Longitude)).ToList();

    queries.Add(sqs => sqs
        .GeoPolygon(r => r
            .Field(f => f.Position)
            .ValidationMethod(GeoValidationMethod.Strict)
            .Points(pointsList)));
}

还有这个:

queries.Add(sqs => sqs.Bool(b=>b.Should(bs => GeoPolygonShouldQuery(bs, area))));

private static QueryContainer GeoPolygonShouldQuery(QueryContainerDescriptor<MySearchableObject> bs, FilterArea area)
{
     return bs
    .GeoPolygon(r => r
        .Field(f => f.Position)
        .ValidationMethod(GeoValidationMethod.Strict)
        .Points(area.Coordinates.Values.Select(p => new GeoLocation(p.Latitude, p.Longitude))));
}

我将如何解决为列表中的每个区域附加一个应该子句?

与此类似:

queries.Add(sqs => sqs.Bool(b => b.Should(
                bs => bs.GeoPolygon(r => r.Field(f => f.Position)
                    .Points(areas.ElementAt(0).Coordinates.Values.Select(p => new GeoLocation(p.Latitude, p.Longitude)).ToList())),
                bs => bs.GeoPolygon(r => r.Field(f => f.Position)
                    .Points(areas.ElementAt(1).Coordinates.Values.Select(p => new GeoLocation(p.Latitude, p.Longitude)).ToList()))
                )));
4

1 回答 1

1

经过更多的试验和错误。这似乎解决了这个问题:

var areas = FindFilterAreas(query.Areas);
queries.Add(sqs => sqs.Bool(b => b.Should(MyQueries(b, areas))));

private static List<Func<QueryContainerDescriptor<SearchableSituation>, QueryContainer>> MyQueries(BoolQueryDescriptor<SearchableSituation> qd, IEnumerable<FilterArea> areas)
{
    List<Func<QueryContainerDescriptor<SearchableSituation>, QueryContainer>> queries = new List<Func<QueryContainerDescriptor<SearchableSituation>, QueryContainer>> { };
    foreach (var area in areas)
    {
        var pointsList = area.Coordinates.Values.Select(p => new GeoLocation(p.Latitude, p.Longitude)).ToList();

        queries.Add(sqs => sqs
            .GeoPolygon(r => r
                .Field(f => f.Position)
                .ValidationMethod(GeoValidationMethod.Strict)
                .Points(pointsList)));
    }    

return queries;

}

于 2020-05-29T14:34:34.393 回答