title: does elasticsearch have compound indexes?
body: <p>i'm wondering if elasticsearch needs to have compound indexes defined a priori. by a compound index, i mean something like what mongodb has.</p>
  
  <blockquote>
    <p>db.collection.ensureIndex( { field1: 1, field2: 1, field3: 1 } )</p>
  </blockquote>
  
  <p>or something like what mysql db has.</p>
  
  <blockquote>
    <p>create index adhoc_index on mytable(field1, field2, field3);</p>
  </blockquote>
  
  <p>so the data i am dealing with is very flat (most of it is just csv format). it looks like the following (for completeness).</p>
  
  <blockquote>
    <p>field1, field2, ..., fieldN</p>
  </blockquote>
  
  <p>the number of fields is arbitrary. one dataset may have 10 fields, another 20, another 1000.  i basically convert each row into a JSON document that looks like the following.</p>
  
  <pre><code>{
   "field1" : "value1",
   "field2" : "value2",
   ...
   "fieldN" : "valueN"
  }
  </code></pre>
  
  <p>denote A, B, and C as three mutually exclusive subsets of the fields: {field1, field2, ..., fieldN}. at any given time, i have to build a dynamic query that filters the records for A=a, B=b, and C=c. </p>
  
  <p>for example, </p>
  
  <ul>
  <li>A = {field1}, B = {field2, field3}, C = {field6}</li>
  <li>A = {field2}, B = {field1}, C = {field1000, field50}</li>
  </ul>
  
  <p>so my elasticsearch DSL query may look something like the following (not sure if this is correct myself, but just to illustrate).</p>
  
  <pre><code>"bool" : {
   "must" : [
    {"term" : { "field1" : "val1" },
    {"term" : { "field2" : "val2" },
    {"term" : { "field3" : "val3" },
    {"term" : { "field4" : "val4" }
   ]
  }
  </code></pre>
  
  <p>basically, this query says, "give me all the documents with field1=val1, field2=val2, field3=val3, field4=val4". </p>
  
  <p>the reason why i ask this about elasticsearch is because i could not find a clear answer searching on the internet about compound indexes. are they even needed? </p>
  
  <p>i'm evaluating mongodb and mysql as well, and i don't think they will work well with my situation simply because these compound/composite indexes have to be defined a priori, and i won't have that information until runtime which group of fields need to be indexed together to optimize the query speed. of course, with mysql, once i find out which group of fields need to be indexed together (and in which order), i can go back an create the index, but that may take a long time if the dataset is large (number of rows > 1 million). </p>
  
  <p>do i simply get this compound index feature out of the box with elastic search? meaning, i won't even have to touch the index mapping file/definition? </p>