简体   繁体   中英

Azure Cosmos DB Query Within not supported

I have created an Azure Function (HTTP triggered) that is running a spatial query against a Cosmos DB using Linq. It returns rock elements that are within a geometric polygon boundary.

When running this, I get the following error:

Exception while executing function: example One or more errors occurred. (Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6) Method 'Within' is not supported., Windows/10.0.14393 documentdb.netcore-sdk/2.11.6

I have checked I am using all the latest NuGet library versions. Can someone give me a hint on what else to check? This is the code:

public static class Example
{
    [FunctionName("example")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "example")] HttpRequest req,
        [CosmosDB(
            databaseName: "a",
            collectionName: "b",
            ConnectionStringSetting = "CosmosDBConnection")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"GetRockLocationsSpatial for coordinates");
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("a", "b");
            Polygon rectangularArea = new Polygon(
                new[]
                {
                    new LinearRing(new [] {
                        new Position(100, 50),
                        new Position(130, 50),
                        new Position(130, 40),
                        new Position(100, 40),
                        new Position(100, 50)
                    })
                });
            var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri).Where(a => a.location.Within(rectangularArea)).AsEnumerable();

            foreach (RockLocationDocument rock in rocks)
            {
                Console.WriteLine("\t" + rock);
            }

            return new OkObjectResult(JsonConvert.SerializeObject(rocks));
        }
    }
}

I'm not sure what causes your error. But I can get the result with your code. You can follow this and have a try.

RockLocationDocument.cs

using Microsoft.Azure.Documents.Spatial;

namespace example
{
    public class RockLocationDocument
    {

        public string id { get; set; }

        public Point location { get; set; }
    }
}

Founction.cs

I pass FeedOption within CreateDocumentQuery method.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Spatial;
using System.Linq;

namespace example
{
    public static class Example
{
    [FunctionName("example")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "example")] HttpRequest req,
        [CosmosDB(
            databaseName: "Test",
            collectionName: "site",
            ConnectionStringSetting = "sogo_DOCUMENTDB")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"GetRockLocationsSpatial for coordinates");
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("Test", "site");
            Polygon rectangularArea = new Polygon(
                new[]
                {
                    new LinearRing(new [] {
                        new Position(100, 50),
                        new Position(130, 50),
                        new Position(130, 40),
                        new Position(100, 40),
                        new Position(100, 50)
                    })
                });

            var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri,new FeedOptions { EnableCrossPartitionQuery = true }).Where(a => a.location.Within(rectangularArea)).AsEnumerable();

            foreach (RockLocationDocument rock in rocks)
            {
                Console.WriteLine("\t" + JsonConvert.SerializeObject(rock));
            }

            return new OkObjectResult(JsonConvert.SerializeObject(rocks));
        }
    }
}

Library:

在此处输入图像描述

Test by using postman:

在此处输入图像描述

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM