Skip to content
wade.one

wade.one

wade womersley – york based software engineer

  • Home
  • 2015
  • July
  • 6
  • Cloudant (IBM) Query – Indexing arrays directly using Erlang

Cloudant (IBM) Query – Indexing arrays directly using Erlang

Posted on July 6, 2015 By Wade No Comments on Cloudant (IBM) Query – Indexing arrays directly using Erlang
Programming

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.

Share:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: The Crystal Maze is Back – and it’s for all of us!
Next Post: Back to the … Theatres ❯

You may also like

Programming
Useful tidbits for Raspberry pi users on Raspian – aka things I wish I knew quicker/easier
April 2, 2016
Programming
Cloudant’s Rather Awesome dbcopy for re-reducing the output of existing map/reduce data.
June 23, 2013
Ego-centric
Heterogeneous Parallel Programming – Completed and Certified!
May 15, 2015
Programming
For PuTTY Users – PuTTY Connection Manager
November 18, 2009

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Copyright © 2025 wade.one.

Theme: Oceanly News Dark by ScriptsTown