使用use_index进行_find查询时遇到问题

问题描述 投票:2回答:2

我正在尝试在CouchDB 2.x中使用类似于MongoDB的_find查询:http://docs.couchdb.org/en/2.1.0/api/database/find.html

[以下内容,我收到警告,我应该使用索引。

curl -k -X POST "$COUCHDB_HOST/vacation-at-ibm/_find" \
>      -H "Content-Type: application/json" \
>      -w "\nhttp_code=%{http_code}\n" \
>      --data-binary '
> {
>     "selector": {
>         "ownerId": {"$eq": "1A4061897"}
>     }
> }
> '

{"warning":"no matching index found, create an index to optimize query time",

"docs":[
{...}
http_code=200

但是当我添加use_index选项时,出现一个错误,它找不到我的索引:

curl -k -X POST "$COUCHDB_HOST/vacation-at-ibm/_find" \
>      -H "Content-Type: application/json" \
>      -w "\nhttp_code=%{http_code}\n" \
>      --data-binary '
> {
>     "use_index": [
>         "_design/EventDocument",
>         "events-by-ownerId"
>     ],
>     "selector": {
>         "ownerId": {"$eq": "1A4061897"}
>     }
> }
> '
{"error":"unknown_error","reason":"Unknown Error: mango_idx :: {no_usable_index,no_index_matching_name}"}

我假设我应该指定设计文档关键字和视图名称。但这是一个猜测,因为我发现use_index选项绝对没有有用的文档。没有实际尝试使用它的示例。这是我的设计文档:

{
  "_id": "_design/EventDocument",
  "_rev": "1-115570169dec7d32845c3b7c6e6978fe",
  "language": "javascript",
  "views": {
    "events-by-ownerId": {
      "map": "function(doc) { if (doc.docType == 'event' && doc.ownerId) { var firstDate = null;  if (doc.date) { firstDate = doc.date;  }  emit([doc.ownerId, firstDate], doc); }}"
    }
  }
}

有人知道如何使用此选项吗?我是否为设计文档和索引使用了错误的值?

couchdb
2个回答
3
投票

因此,当他们说“创建索引”时,他们指的是/db/_index端点。

您需要通过此过程创建索引。您可以指定存储索引的位置,否则,CouchDB将创建一个设计文档并将索引存储在其中。


2
投票

我也对此功能感到困惑。要在/db/_index中使用索引,您必须通过_find端点创建一个芒果语言索引。 _index字段必须等于包含已创建索引的设计文档的名称,并且不需要前导use_index;如果在创建索引时未指定_design/,则设计文档的名称将为uuid。如果设计文档ddoc具有多个索引,则可以将其称为some-indexes。没有设计文档名称,就不能仅通过索引名称来引用索引。即使您指定"use_index": "some-indexes/name",也不会使用索引,除非您的查询在索引定义中包括所有use_index

© www.soinside.com 2019 - 2024. All rights reserved.