CouchDB display / reduce any document property at runtime?

I came from the SQL world, where the search is performed using several properties of the object (published = TRUE or user_id = X), but not anywhere (due to the cache level 1: 1). It seems that the document database will be a good fit for my data.

I am trying to find out if there is a way to transfer one (or more) object properties to the CouchDB map / reduce the function to find the corresponding documents in the database without creating dozens of views for each type of document.

Is it possible to pass the desired property keys of the document to match at runtime of CouchDB and return the objects that match (or the score of the object that matches the pagination)?

For example, on one page, I need all messages with doc.user_id from X, which are doc.published . On another page, I may need all the documents with doc.tags[] with the tag "sport".

+4
source share
3 answers

You can create a view that iterates over the keys in the document and returns the key [propertyName, propertyValue] - this way you create one index with EVERYTHING prop / value in it. It would be massive, I don’t know how performance will build, and disk usage (possibly bad).

The map function will look something like this:

 // note - totally untested, my CouchDB fu is rusty function(doc) { for(prop in doc) { emit([prop, doc[prop]], null); } } 

It works for the main case of simple properties and can be extended to be smart with respect to arrays and allocate a prop / value pair for each element of the array. This will allow you to process tags.

To request it, set [prop] as the request key in the view.

+6
source

In principle, no.

The key difference between something like Couch and SQL DB is that the only way to query CouchDB is essentially through views / indexes. Indexes in SQL are optional. They exist (mainly) to increase productivity. For example, if you have a small database, your application will run fine on SQL with 0 indexes. (This may be a problem with unique constraints, but this detail.)

The common point is that part of the query processor in the SQL database includes other methods of accessing data outside of simple indexes, in particular, table scans, merge joins, etc.

Couch does not have a query processor. It has views (defined by JS) used to define B-Tree indexes.

What is it. This is the hammer of the Sofa. This is a good hammer. This has continued in the world of data processing for 40 years.

Indexes are somewhat expensive to create in Couch (based on the amount of data), so the "temporary views" are disapproved. And they have maintenance costs, so opinions should be a conscious design element in your database. At the same time, they are slightly more powerful than regular SQL indexes.

You can easily add your own request processing on top of Couch, but this will be more for you. You can create several selected submissions according to the most popular or selective criteria, and then filter the received documents according to other criteria in your own code. Yes, you have to do this, so you need to ask whether it is worth the effort to spend more than any of the benefits Couch thinks (HTTP API, replication, secure, always consistent data storage, etc.). ) By the SQL solution.

+2
source

I ran into a similar problem like this and created a quick workaround using CouchDB-Python (which is a great library). This is not a good solution (goes against the principles of CouchDB), but it works.

CouchDB-Python provides you with a "Query" function that allows you to "perform a temporary temporary view against the database." You can read about it here.

I have that I store the javascript function as a string in python and combine it with the variable names that I define in Python.

In some_function.py

 variable = value # Map function (in javascript) map_fn = """function(doc) { <javascript code> var survey_match = """ + variable + """; <javascript code> """ # Iterates through rows for row in db.query(map_fn): <python code> 

Of course, this is ugly and probably breaks a bunch of CouchDB philosophy, but it works.

D

0
source

All Articles