DocumentDb “where” clause with mathematical expression

别等时光非礼了梦想. 提交于 2019-12-13 01:28:14

问题


I would like to understand how to create query where clauses on DocumentDB with mathematical comparator inside.

For example, I used this demonstrator to understand how to make a "greater than" comparaison : expression AND food.version > 0 seems to work very well.

Here is under what I tryed onto portal.azure.com documentdb query explorer and the results. I don't understand why I got an error in some cases(QUERY3), and (in option) how to get error details on portal.azure.com ?!

Tested:

>>> QUERY1 >>
SELECT d.id,
       d.name, 
       d.lastUpdateTime
FROM d 
>>> RESULT1 >>
[
  {
    "id": "558d6007b909e8dfb2286e7b",
    "name": "cSimpleSIMS_ici",
    "lastUpdateTime": 1435589982672
  },
  {
    "id": "558d6009b909e8df18296e7b",
    "name": "didier",
    "lastUpdateTime": 1435330811285
  },
  {
    "id": "558d600ab909e8df28296e7b",
    "name": "cDoubleSIMD_ici",
    "lastUpdateTime": 1435331176750
  },
  {
    "id": "558d600bb909e8df55296e7b",
    "name": "george",
    "lastUpdateTime": 1435330813519
  }
  (...)
]

>>> QUERY2 >>
SELECT d.id,
       d.name, 
       d.lastUpdateTime
FROM d 
WHERE (d.name='george')

>>> RESULT2 >>
[
  {
    "id": "558d600bb909e8df55296e7b",
    "name": "george",
    "lastUpdateTime": 1435330813519
  }
]

>>> QUERY3 >>
SELECT d.id,
       d.name, 
       d.lastUpdateTime
FROM d 
WHERE (d.lastUpdateTime > 14)
>>> RESULT3 IN ERROR!

>>> QUERY4 >>
SELECT d.id,
       d.name, 
       d.lastUpdateTime
FROM d 
WHERE (d.name='george' AND d.lastUpdateTime > 14)

>>> RESULT4 >>
[
  {
    "id": "558d600bb909e8df55296e7b",
    "name": "george",
    "lastUpdateTime": 1435330813519
  }
]


>>> QUERY5 >>
SELECT d.id,
       d.name, 
       d.lastUpdateTime
FROM d 
WHERE (d.name='george' AND d.lastUpdateTime > 1435330813519)

>>> RESULT5 >>
[]

回答1:


Here's the gist...

Today, all JSON properties in DocumentDB get automatically indexed by a Hash index; which means queries with equality operators (e.g. WHERE d.name= "george") are extremely fast.

On the other hand, range queries (e.g. WHERE d.lastUpdateTime > 14) require a range index to operate efficiently. Without a range index, the range query will require a scan across all documents (which we allow if the header, x-ms-documentdb-query-enable-scan, is passed in by the request).

The queries you issued that had both a equality and range filter (e.g. WHERE d.name='george' AND d.lastUpdateTime > 14) succeeded, because the equality filter greatly narrowed down the set of documents to scan through.

TL;DR: There are two things you can do here to get rid of the error:

  1. Create a custom index policy to add a range index for numeric types. The documentation for indexing policies can be found here.

  2. Issue your query programmatically (not through the Azure Portal) to set the x-ms-documentdb-query-enable-scan header to allow scans on range queries.

P.S. I will push to improve the Azure Portal for you.

Now... there appear to be a few issues in the Azure Portal - which I will push to get fixed for you.

Bug: Exception message is truncated

Looks like the meaningful part of the exception message gets truncated out when using the Azure Portal - which is no bueno. What SHOULD have been displayed is:

Microsoft.Azure.Documents.DocumentClientException: Message: {"Errors":["An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request."]}

Missing Feature: Enabling scans in query explorer

There ability to set the x-ms-documentdb-query-enable-scan header is currently not exposed in the Azure Portal's query explorer. We will add a checkbox or something for this.




回答2:


To add to aliuy's answer, we're working on a change that will improve the developer experience here - Default indexing policy for numbers will be changed from Hash to Range index, so you do not need the header or override indexing policy in order to perform range queries.



来源:https://stackoverflow.com/questions/31209970/documentdb-where-clause-with-mathematical-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!