1

I am new to Elasticsearch and hope to know whether this is possible.

Basically, I have the values in the "code" property for multiple documents. Each document has a unique value in this property. Now I have the codes of multiple documents and hope to retrieve them in one request by supplying multiple codes.

Is this doable in Elasticsearch?

Regards.

Edit

This is the mapping of the field:

"code" : { "type" : "string", "store": "yes", "index": "not_analyzed"},

Two example values of this property:

0Qr7EjzE943Q
GsPVbMMbVr4s

What is the ES syntax to retrieve the two documents in ONE request?

curious1
  • 14,155
  • 37
  • 130
  • 231
  • Yeah, it's possible. That's sort of what ES does. If you'll post some example data and an example query I'll give you a quick demonstration. – Sloan Ahrens Mar 14 '15 at 23:07

1 Answers1

4

First, you probably don't want "store":"yes" in your mapping, unless you have _source disabled (see this post).

So, I created a simple index like this:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "code": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

added the two docs with the bulk API:

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"code":"0Qr7EjzE943Q"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"code":"GsPVbMMbVr4s"}

There are a number of ways I could retrieve those two documents. The most straightforward, especially since the field isn't analyzed, is probably a with terms query:

POST /test_index/_search
{
    "query": {
        "terms": {
           "code": [
              "0Qr7EjzE943Q",
              "GsPVbMMbVr4s"
           ]
        }
    }
}

both documents are returned:

{
   "took": 21,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.04500804,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.04500804,
            "_source": {
               "code": "0Qr7EjzE943Q"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 0.04500804,
            "_source": {
               "code": "GsPVbMMbVr4s"
            }
         }
      ]
   }
}

Here is the code I used:

http://sense.qbox.io/gist/a3e3e4f05753268086a530b06148c4552bfce324

Community
  • 1
  • 1
Sloan Ahrens
  • 8,588
  • 2
  • 29
  • 31