Scott Feinberg Scott Feinberg - 5 days ago 6
PHP Question

Using Elastic Search to Query inside arrays in CouchDB

My CouchDB database is structured like this:

"custom_details": {
"user_education": [
{
"device_id": "358328030246627",
"college_name": "College",
"college_year": "2014"
},
]
}

"custom_details_1": {
"user_education": [
{
"device_id": "358328030246627",
"college_name": "College",
"college_year": "2014"
},
]
}


I have a lot of arrays within arrays. What I'm trying to do use Elasticsearch to search and find terms, regardless of where it's sitting in an array. Is that possible?

I've been going through the examples on here and haven't quite found what I'm looking for. I've tried using Elastica, the PHP Wrapper, but without fully understanding how to do this with REST, I'm lost. Is it even possible to search for data without knowing the field?

Answer

In Lucene, you could create a document instance for each device id:

public void indexRecord(CouchDBRecord rec) {
    Document doc = new Document();
    doc.add(new Field("device_id", rec.device_id, Store.YES, Index.NOT_ANALYZED));
    doc.add(new Field("college_name",  rec.college_name, Store.YES, Index.ANALYZED));
    doc.add(new Field("college_year", rec.college_year.toString(), Store.YES, Index.NOT_ANALYZED));
    this.writer.addDocument(doc);
}

This will allow you to search by keywords in the college name, or by exact device id or year, or some combination thereof.

Comments