按值对CouchDB视图进行排序 [英] Sorting CouchDB Views By Value

查看:133
本文介绍了按值对CouchDB视图进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试CouchDB,以了解它如何处理记录一些搜索结果.我想做的是产生一个视图,在这里我可以从结果中产生最重要的查询.此刻我有这样的东西:

示例文档部分

{
  "query": "+dangerous +dogs",
  "hits": "123"
}

地图功能 (不完全是我所需要/想要的,但它足以进行测试)

function(doc) {
  if (doc.query) {
    var split = doc.query.split(" ");
    for (var i in split) {
      emit(split[i], 1);
    }
  }
}

减少功能

function (key, values, rereduce) {
  return sum(values);
}

现在,这将使我得到一种格式,其中查询项是键,而该项的计数在右侧,这很棒.但我希望它按值而不是键排序.从它的声音来看,CouchDB尚无法做到这一点.

任何人对我如何在有查询词和&的有序版本的情况下如何查看视图有任何想法.他们的相关计数?我对CouchDB非常陌生,只是想不出如何编写所需的功能.

解决方案

确实没有简单的答案.但是有几种模式.

  1. http://wiki.apache.org/couchdb/View_Snippets#Retrieve_the_top_N_tags.我个人不喜欢这样,因为他们承认这是一种脆弱的解决方案,并且代码看起来也不轻松.

  2. Avi的答案是对应用程序中的内存进行排序.

  3. couchdb-lucene 似乎每个人最终都发现自己需要!! p>

  4. 我喜欢克里斯在阿维的语录中说的话.放松.在CouchDB中,数据库是轻量级的,擅长为您提供数据的独特视角.这些天来,嗡嗡声全是关于过滤复制的,它是关于将数据的子集切成一个单独的数据库.

    无论如何,基础很简单.您从视图输出中获取.rows,并将其插入到单独的数据库中,该数据库仅发出计数键.另一个技巧是编写一个非常简单的_list函数.列出将原始沙发输出渲染"为不同格式.您的_list函数应该输出

    { "docs":
        [ {..view row1...},
          {..view row2...},
          {..etc...}
        ]
    }
    

    这将是完全按照_bulk_docs API要求的方式格式化视图输出的格式.现在,您可以将卷发直接传送到另一个卷发中:

    curl host:5984/db/_design/myapp/_list/bulkdocs_formatter/query_popularity \
     | curl -X POST host:5984/popularity_sorter/_design/myapp/_view/by_count
    

  5. 实际上,如果您的列表函数可以处理所有文档,则可以让它自己对它们进行排序,然后将它们返回给排序后的客户端.

I'm testing out CouchDB to see how it could handle logging some search results. What I'd like to do is produce a view where I can produce the top queries from the results. At the moment I have something like this:

Example document portion

{
  "query": "+dangerous +dogs",
  "hits": "123"
}

Map function (Not exactly what I need/want but it's good enough for testing)

function(doc) {
  if (doc.query) {
    var split = doc.query.split(" ");
    for (var i in split) {
      emit(split[i], 1);
    }
  }
}

Reduce Function

function (key, values, rereduce) {
  return sum(values);
}

Now this will get me results in a format where a query term is the key and the count for that term on the right, which is great. But I'd like it ordered by the value, not the key. From the sounds of it, this is not yet possible with CouchDB.

So does anyone have any ideas of how I can get a view where I have an ordered version of the query terms & their related counts? I'm very new to CouchDB and I just can't think of how I'd write the functions needed.

解决方案

It is true that there is no dead-simple answer. There are several patterns however.

  1. http://wiki.apache.org/couchdb/View_Snippets#Retrieve_the_top_N_tags. I do not personally like this because they acknowledge that it is a brittle solution, and the code is not relaxing-looking.

  2. Avi's answer, which is to sort in-memory in your application.

  3. couchdb-lucene which it seems everybody finds themselves needing eventually!

  4. What I like is what Chris said in Avi's quote. Relax. In CouchDB, databases are lightweight and excel at giving you a unique perspective of your data. These days, the buzz is all about filtered replication which is all about slicing out subsets of your data to put in a separate DB.

    Anyway, the basics are simple. You take your .rows from the view output and you insert it into a separate DB which simply emits keyed on the count. An additional trick is to write a very simple _list function. Lists "render" the raw couch output into different formats. Your _list function should output

    { "docs":
        [ {..view row1...},
          {..view row2...},
          {..etc...}
        ]
    }
    

    What that will do is format the view output exactly the way the _bulk_docs API requires it. Now you can pipe curl directly into another curl:

    curl host:5984/db/_design/myapp/_list/bulkdocs_formatter/query_popularity \
     | curl -X POST host:5984/popularity_sorter/_design/myapp/_view/by_count
    

  5. In fact, if your list function can handle all the docs, you may just have it sort them itself and return them to the client sorted.

这篇关于按值对CouchDB视图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆