Really short post this one on indexing using Cloudant Query to index arrays in your document, e.g. documents that have a field like this:
{ "ids": [ "alpha", "bravo", "charlie" ] }
I was reading the documentation and just could not get it to index these items using anything but the default _all_docs indexer (don’t use this for large databases!) but then I came across the source code for Mango and hidden in the tests and the implementation was the answer I needed.
To create an index, as the documentation states, you just need to POST to /dbname/_index with something like:
{ "type": "text", "index": { "default_analyzer": "keyword", "fields": [ {"name": "ids", "type": "string"} ] } }
However, if you try this for an array and perform a search (POST to /dbname/_find {“selector”:{“ids”:”alpha”}}) you’ll get no results! So how do you do it? Ridiculously simply, just change ids to ids.[]:
{ "type": "text", "index": { "default_analyzer": "keyword", "fields": [ {"name": "ids.[]", "type": "string"} ] } }
The query however isn’t just “ids”: “alpha”, it’s a bit more complex, not much, but a bit: {“selector”:{“ids”:{“$elemMatch”:{“$eq”:”alpha”}}}}.
There may be a better way (without writing your own function(doc){index(“”);…}) but for now, this works and uses native Erland as opposed to Javascript.