【问题标题】:Azure Search - Query Collection of GeographyPointAzure 搜索 - GeographyPoint 的查询集合
【发布时间】:2020-04-23 10:31:22
【问题描述】:

我正在尝试从我的索引中查询给定范围内的集合 GeographyPoint。我正在使用 Azure 搜索。

我的索引包含 GeographyPoint 列表

public class Parent
{
     public List<Child> Coordinates {get; set;}
}


public class Child
{ 
     public GeographyPoint Coordinates { get; set; }
}

我知道索引上是否有一个名为 location 的属性,您可以通过以下方式查询它:

$filter=geo.distance(location, geography'POINT(-82.51571 31.89063)') le 0.1524

但是你如何查询一个集合呢?我尝试使用任何/所有过滤器,例如:

$filter=geo.distance(IndexedCompany/Offices/any(c: c/PhysicalAddress/Coordinates), geography'POINT(-82.51571 31.89063)') le 0.1524

但我收到错误:“无效表达式:Any/All 查询表达式必须计算为单个布尔值。\r\n参数名称:$filter”

任何有关如何查询 GeographyPoint 集合的帮助将不胜感激。

【问题讨论】:

    标签: odata azure-cognitive-search azure-search-.net-sdk


    【解决方案1】:

    问题在于您如何构建过滤器。让我们使用类型来分解它,因为这是查看哪里出错的最简单方法。

    $filter=geo.distance(IndexedCompany/Offices/any(c: c/PhysicalAddress/Coordinates), geography'POINT(-82.51571 31.89063)') le 0.1524

    从外向内开始,过滤器中最顶层的表达式必须是布尔值,这在此处检查是因为您将geo.distance(返回Edm.Double)与使用le 的文字距离进行比较。所以那部分很好。

    接下来,我们来看看geo.distance。它需要两个Edm.GeographyPoint 类型的点并返回一个Edm.Double。第二个参数是一个文字点,所以这很好,但第一个参数不是。让我们接下来看看。

    这里geo.distance 的第一个参数是表达式IndexedCompany/Offices/any(c: c/PhysicalAddress/Coordinates)。尽管它实际上是一个运算符,但您可以将any 视为一种返回布尔值的函数。这将是一个问题,因为geo.distance 期待的是一个点,而不是布尔值。这是我们发现这里出了什么问题的第一个线索,但它实际上并不能解释您看到的错误消息。让我们进一步挖掘以了解这一点。

    暂时假设表达式IndexedCompany/Offices/any(c: c/PhysicalAddress/Coordinates) 是您的整个过滤器。您仍然会收到相同的错误消息:"Invalid expression: The Any/All query expression must evaluate to a single boolean value." 要了解原因,请考虑 anyall 在类型方面的预期。每一个都迭代某种类型的集合,将 lambda 表达式形式的谓词应用于每个元素。本例中的 lambda 表达式是 c/PhysicalAddress/Coordinates,(我假设)是 Edm.GeographyPoint,而不是布尔值。

    退后一步,考虑一下这里涉及的基数会有所帮助。每个文档都包含一个公司,该公司有许多办公室,每个办公室都有一个地理位置。我假设目标是匹配整个文档,以防这些坐标中的任何一个与过滤器匹配。在这种情况下,您真的只需要重新排序查询的各个部分,以便类型和基数对齐:

    $filter=IndexedCompany/Offices/any(office: geo.distance(office/PhysicalAddress/Coordinates, geography'POINT(-82.51571 31.89063)') le 0.1524)

    过滤器仍然是一个布尔表达式,因为那是any 的类型。传递给 any 的 lambda 获取一个 Office,将 geo.distance 应用于其地理位置,并将距离与所需值进行比较。现在所有类型都进行了检查,并且迭代发生在正确的位置。

    示例:search=*

    "value": [
        {
            "@search.score": 1,
            "id": "myid1",
            "pts": [
                {
                    "type": "Point",
                    "coordinates": [ 0, 0 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                },
                {
                    "type": "Point",
                    "coordinates": [ 0, 1 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                }
            ]
        },
        {
            "@search.score": 1,
            "id": "myid2",
            "pts": [
                {
                    "type": "Point",
                    "coordinates": [ 0, 10 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                },
                {
                    "type": "Point",
                    "coordinates": [ 0, 11 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                }
            ]
        }
    ]
    

    示例 $filter=pts/any(pt: geo.distance(pt, geography'POINT(0 2)') le 111.3192394008)

    "value": [
        {
            "@search.score": 1,
            "id": "myid1",
            "pts": [
                {
                    "type": "Point",
                    "coordinates": [ 0, 0 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                },
                {
                    "type": "Point",
                    "coordinates": [ 0, 1 ],
                    "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }
                }
            ]
        }
    ]
    

    【讨论】:

    • 很好的回答解释。真的应该接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2021-06-07
    相关资源
    最近更新 更多