Azure函数与Cosmos DB的通配符搜索

huangapple go评论60阅读模式
英文:

Azure functions with Cosmos DB with wild search

问题

我想要一个“模糊”搜索,我可以获得名字或姓氏。所以可以发送名字为""和姓氏为"Smith",这样我就可以得到所有姓"Smith"的人。

我觉得这部分是正确的,但在标题中使用SQL查询有点奇怪。有更好的方法吗?

[FunctionName("People_Get")]
public IActionResult GetPeople(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request,
HttpRequest req,
[CosmosDB("CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString",
SqlQuery = @"SELECT c.id,
c.FirstName,
c.Surname
FROM c.FirstName
WHERE c.FirstName == firstName OR c.FirstName == surname")]
IEnumerable<Models.People> peopleReviews,
ILogger log)
{
return new OkObjectResult(peopleReviews);
}

英文:

I want a 'wild' search where I get FirstName OR Surname. so it would be possible to send a FirstName of "" and Surname of "Smith" so I get everyone with the surname "Smith".

I feel this is partly correct, but having a sql query in the header has a slightly off smell. Is there a better way to do this?

 [FunctionName($&quot;People_Get&quot;)]
 public IActionResult GetPeople(
 [HttpTrigger( AuthorizationLevel.Anonymous, &quot;get&quot;, Route = &quot;Review/{firstName}/{surname}&quot;)]
 PeopleRequest request,
 HttpRequest req,
 [CosmosDB( &quot;CosmosDB&quot;, &quot;PeopleContainer&quot;, Connection = &quot;CosmosDbConnectionString&quot;,
   SqlQuery = @&quot;SELECT 	c.id ,
						c.FirstName ,
						c.Surname 
			    FROM c.FirstName
				WHERE c.FirstName == firstName OR c.FirstName == surname&quot;)]
IEnumerable&lt;Models.People&gt; peopleReviews,
 ILogger log)
 {        
	return new OkObjectResult(peopleReviews);
 }

答案1

得分: 1

以下是已翻译的部分:

Azure functions with Cosmos DB with wild search

The code is correct, but there are a few changes to be done.

  1. The SqlQuery property in the CosmosDB attribute should use string interpolation to include the values of the firstName and surname parameters in the query, rather than hardcoding them.

  2. Use the 'LIKE' operator in the WHERE clause instead of '==' to match partial strings.

  3. And also, you can add a condition to check if either firstName or surname is null or empty, and return an error response in that case.

The below is the updated code:

public static class Function1
    {
        [FunctionName("People_Get")]
        public IActionResult GetPeople([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Review/{firstName}/{surname}")]
PeopleRequest request, HttpRequest req, [CosmosDB("CosmosDB", "PeopleContainer", Connection = "CosmosDbConnectionString")]
IEnumerable<Models.People> peopleReviews, ILogger log)
        {
            if (string.IsNullOrEmpty(request.firstName) && string.IsNullOrEmpty(request.surname))
            {
                return new BadRequestObjectResult("Please provide either a first name or a surname.");
            }

            var query = from p in peopleReviews
            where (string.IsNullOrEmpty(request.firstName) || p.FirstName.Contains(request.firstName))
            && (string.IsNullOrEmpty(request.surname) || p.Surname.Contains(request.surname))
            select p;

            return new OkObjectResult(query);

        }
    }
英文:

>Azure functions with Cosmos DB with wild search

The code is correct, but there are a few changes to be done.

  1. The SqlQuery property in the CosmosDB attribute should use string interpolation to include the values of the firstName and surname parameters in the query, rather than hardcoding them.

  2. Use the 'LIKE' operator in the WHERE clause instead of '==' to match partial strings.

  3. And also, you can add a condition to check if either firstName or surname is null or empty, and return an error response in that case.

The below is the updated code:

public static class Function1
    {
        [FunctionName(&quot;People_Get&quot;)]
        public IActionResult GetPeople([HttpTrigger(AuthorizationLevel.Anonymous, &quot;get&quot;, Route = &quot;Review/{firstName}/{surname}&quot;)]
PeopleRequest request, HttpRequest req, [CosmosDB(&quot;CosmosDB&quot;, &quot;PeopleContainer&quot;, Connection = &quot;CosmosDbConnectionString&quot;)]
IEnumerable&lt;Models.People&gt; peopleReviews, ILogger log)
        {
            if (string.IsNullOrEmpty(request.firstName) &amp;&amp; string.IsNullOrEmpty(request.surname))
            {
                return new BadRequestObjectResult(&quot;Please provide either a first name or a surname.&quot;);
            }

            var query = from p in peopleReviews
            where (string.IsNullOrEmpty(request.firstName) || p.FirstName.Contains(request.firstName))
            &amp;&amp; (string.IsNullOrEmpty(request.surname) || p.Surname.Contains(request.surname))
            select p;

            return new OkObjectResult(query);

        }
    }

huangapple
  • 本文由 发表于 2023年2月6日 06:40:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定