Search code examples
couchdbcloudant

cloudant searching index by multiple values


Cloudant is returning error message:

{"error":"invalid_key","reason":"Invalid key use-index for this request."}

whenever I try to query against an index with the combination operator, "$or".

A sample of what my documents look like is:

{
  "_id": "28f240f1bcc2fbd9e1e5174af6905349",
  "_rev": "1-fb9a9150acbecd105f1616aff88c26a8",
  "type": "Feature",
  "properties": {
    "PageName": "A8",
    "PageNumber": 1,
    "Lat": 43.051523,
    "Long": -71.498852
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -71.49978935969642,
          43.0508382914137
        ],
        [
          -71.49978564033566,
          43.052210148524
        ],
        [
          -71.49791499857444,
          43.05220740550381
        ],
        [
          -71.49791875962663,
          43.05083554852429
        ],
        [
          -71.49978935969642,
          43.0508382914137
        ]
      ]
    ]
  }
}

The index that I created is for field "properties.PageName", which works fine when I'm just querying for one document, but as soon as I try for multiple ones, I would receive the error response as quoted in the beginning.

If it helps any, here is the call: POST https://xyz.cloudant.com/db/_find

request body:

{
  "selector": {
    "$or": [
      { "properties.PageName": "A8" },
      { "properties.PageName": "M30" },
      { "properties.PageName": "AH30" } 
    ]
  },
  "use-index": "pagename-index"
}

Solution

  • In order to perform an $or query you need to create a text (full text) index, rather than a json index. For example, I just created the following index:

    {
      "index": {
        "fields": [
          {"name": "properties.PageName", "type": "string"}
        ]
      },
      "type": "text"
    }
    

    I was then be able to perform the following query:

    {
      "selector": {
        "$or": [
          { "properties.PageName": "A8" },
          { "properties.PageName": "M30" },
          { "properties.PageName": "AH30" }
        ]
      }
    }