couchdb 中两个关键范围的问题

发布于 2024-08-16 12:26:54 字数 719 浏览 4 评论 0原文

我在坐标系中无法获得正确的结果。 为了解释我的系统,我有一个简单的数据库,其中包含 x_axis、y_axis 和 name 列。我不需要获取所有数据,我只需要显示其中的一部分。

例如,我有一个 10:10 的坐标系(意味着从 x_axis -10 到 10 和从 y_axis -10 到 10),我只想显示 49 个坐标。在 sql 查询中我可以这样做: “从 x_axis >= -3 和 x_axis <= 3 且 y_axis >= -3 y_axis <= 3 的坐标中选择 *”

我尝试了此功能但没有成功:

   "by_range": {
       "map": "function(doc) { emit([doc.x_axis, doc.y_axis], doc) }"
   }

by_range?startkey=[-3,-3] &endkey=[3,3]

我得到的错误结果为:

-3x-3 -3x-2 -3x-1 -3x0 -3x1 -3x2 -3x3 <-- 不应显示此部分 --> -3x4 -3x5 -3x6 -3x7 -3x8 -3x9 -3x10 <-- 结尾不应该显示这部分 --> ..... 高达 3x3,

为了让您更好地了解我的项目,这里是我想要制作的屏幕截图:

I'm having problem getting the right results in my coordinate system.
To explain my system, I have this simple database that have x_axis, y_axis and name columns. I don't need to get all the data, I just need to display some part of it.

For example, I have a coordinate system that have 10:10(meaning from x_axis -10 to 10 and from y_axis -10 to 10) and I want to display only 49 coordinates. In sql query I can do it something like this:
"select * from coordinate where x_axis >= -3 and x_axis <= 3 and y_axis >= -3 y_axis <= 3"

I tried this function but no success:

   "by_range": {
       "map": "function(doc) { emit([doc.x_axis, doc.y_axis], doc) }"
   }

by_range?startkey=[-3,-3]&endkey=[3,3]

I got a wrong results of:

-3x-3
-3x-2
-3x-1
-3x0
-3x1
-3x2
-3x3
<-- should not display this part -->
-3x4
-3x5
-3x6
-3x7
-3x8
-3x9
-3x10
<-- end of should not display this part -->
..... up to 3x3

to give you a better understanding of my project here is the screenshot of that I want to be made:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-08-23 12:26:54
by_range?startkey=[-3,-3]&endkey=[3,3]

您将其用作 WHERE 子句。 Couchdb 不理解“startkey”和“endkey”中的值,它只是使用它们来知道何时开始和停止输出结果。

例如,采用以下结果集:

doc1
doc2
doc3
doc4

如果我应用此查询:

?startkey=doc2&endkey=doc3

结果集将为:

doc2
doc3

要在示例中应用范围,我将修改映射函数:

function(doc) {
 if (doc.x <= 3 && doc.x >= -3 && doc.y <= 3 && doc.y >= -3)
   emit([doc.x, doc.y], doc)
}

更新以处理动态范围

根据数据库查询CouchDB方式

“CouchDB 设计可以让您在大型数据集上获得出色的性能。但这意味着您在运行查询时无法将动态参数传递给地图函数。”

因此,要实现动态范围,您需要将键值的输出更改为单个值,以便可以使用 startkey 和 endkey 参数。

function(doc) {
 emit(doc.x * doc.y, doc)
}

使用此过滤器:

by_range?startkey=-9&endkey=9

在您的应用程序中,您可以通过执行类似的操作来计算开始和结束键

startkey = (x1*y1)
endkey = (x2*y2)
by_range?startkey=[-3,-3]&endkey=[3,3]

You are using this like a WHERE clause. Couchdb does not understand the values in "startkey" and "endkey", it just uses them to know when to start and stop outputting results.

For example, take the following result set:

doc1
doc2
doc3
doc4

If I apply this query:

?startkey=doc2&endkey=doc3

The result set would be:

doc2
doc3

To apply a range in your example, I would modify the map function:

function(doc) {
 if (doc.x <= 3 && doc.x >= -3 && doc.y <= 3 && doc.y >= -3)
   emit([doc.x, doc.y], doc)
}

Update to handle dynamic ranges:

According to Database Queries the CouchDB Way:

"The CouchDB design gets you great performance on large data sets. But it means that you cannot pass dynamic parameters to your map function when you run a query."

So to do a dynamic range, you need to alter the output of the key value to a single value so you can use startkey and endkey parameters.

function(doc) {
 emit(doc.x * doc.y, doc)
}

Use this filter:

by_range?startkey=-9&endkey=9

In your application you can calculate the start and end keys by doing something like

startkey = (x1*y1)
endkey = (x2*y2)
留一抹残留的笑 2024-08-23 12:26:54

请参阅以下页面:

基本上有2个选项:

  1. 使用2单独的查询并合并结果。
  2. 使用 couchdb-lucene

See these pages:

Basically 2 options:

  1. Use 2 seprate queries and merge the results.
  2. Use couchdb-lucene
以可爱出名 2024-08-23 12:26:54

您可以执行类似具有多个键的 WHERE 子句之类的操作以及此 文章

You can do something like a WHERE clause with multiple keys and the technique described in this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文