Not A Ninja
Not A Ninja

Reputation: 31

Limit couchdb-lucene results by key / specific field? map?

I have a pretty straight-forward question.

I am using couchdb-lucene to search the full text of my documents.

My documents each have the following fields:

_id

_rev

docID (the unique ID of the document from our system)

title (title of the document)

content (body of the document)

userID (the user that owns the document)

My design document looks likes this:

{
   "_id": "_design/lucene",
   "_rev": "10-770b7044393e067b7024a896ccf3c502",
   "fulltext": {
       "by_all": {
           "index": "function(doc) { var ret=new Document(); ret.add(doc.title); ret.add(doc.content); return ret }"
       },
       "by_title": {
           "index": "function(doc) { var ret=new Document(); ret.add(doc.title); return ret }"
       }
   }
}

I couldn't figure out how to return the docID as part of the results, so I just made the ID equal to the docID for now, but I would like to understand how to return the docID since the design document above only returns the score and ID of the document. I would rather not have to set the ID to the same value as docID just to access it.

When I run a request such as:

localhost:5984/foo/_fti/_design/lucene/by_all?q=example~0.5f

I get back a collection of all documents where the title or content field contains "example", and that's great, but how can I search for "example" in the title and content fields of ONLY the documents that have a specific userID? How can I pass in and use that value?

I want to do something like this:

localhost:5984/foo/_fti/_design/lucene/by_all?q=example~0.5f&userId=123

When I do: localhost:5984/foo/_fti/_design/lucene/by_all?q=example~0.5f it returns documents for all users, what do I need to do to limit the documents only to those belonging to a specific user? I don't want to search every document out of millions when only a few thousand are relevant to a specific user.

Thanks!

p.s. I know I can use a map in views: "map": "function(doc){ emit(doc._id, doc._rev)}"

Is this relevant? Is there some way to use the map within the couchdb-lucene results? I couldn't figure out how to specify which value to use at the key for the map, so it was returning the ID by default, how can the key be set to use a specific field? Maybe map isn't even the right track... I don't know what to do here, I'm very new to both CouchDB and Lucene and don't really quite understand it all yet.

Thanks again!

Upvotes: 3

Views: 1039

Answers (1)

Robert Newson
Robert Newson

Reputation: 4631

This seems straightforward but you'll need to change your index function.

function(doc) {
  var ret=new Document();
  ret.add(doc.title);
  ret.add(doc.content});
  ret.add(doc.userID, {"field":"userID"});
  ret.add(doc.docID, {"field":"docID","store":"yes"});
  return ret;
}

Query with;

localhost:5984/foo/_fti/_design/lucene/by_all?q=example%20AND%20userID:123

This will find all documents with 'example' in the title or the content as long as userID is equal to 123. Additionally, the docID value will be returned in the results.

HTH, B.

Upvotes: 3

Related Questions