title: Spring Data ElasticSearch 4.0.0 - Using wildcards to search in multiple indexes of the same class
body: <p>So i'm a novice in both ElasticSearch and Spring Data, and i've got an assignment to store the html contents of all outgoing e-mails.</p>
  
  <pre><code>@Document(indexName = "email")
  @Mapping(mappingPath = "elastic/mappings/email.json")
  public class EsOutboundEmail extends PartitionedDocument {
  </code></pre>
  
  <p>I've been told to index it by the month, i think it's for freezing old data later on, so i came up with something like this.</p>
  
  <pre><code>public String indexByMonth(AbstractDocument source) {
      final IndexCoordinates cos = elasticSearchMappingInitializer.getOrCreateDatedIndex(source.getClass());
  
      IndexQueryBuilder queryBuilder = new IndexQueryBuilder()
              .withId(source.getId())
              .withObject(source);
  
      return elasticsearchOperations.index(queryBuilder.build(), cos);
  }
  </code></pre>
  
  <p>And the naming goes like this (ignore the "initializer" bean name, that's gonna change):</p>
  
  <pre><code>   public IndexCoordinates getOrCreateDatedIndex(Class&lt;? extends AbstractDocument&gt; clazz) {
         String indexName = getIndexName(clazz);
         indexName = String.format("%s-%s", indexName, now().format(DateTimeFormatter.ofPattern("yyyy-MM")));
         final IndexOperations indexOperations = elasticsearchOperations.indexOps(IndexCoordinates.of(indexName));
         if (!indexOperations.exists()) {
             indexOperations.create();
             indexOperations.createMapping(clazz);
         }
         return IndexCoordinates.of(indexName);
     }
  
     public String getIndexName(Class&lt;? extends AbstractDocument&gt; clazz) {
         if (!clazz.isAnnotationPresent(Document.class)) {
             throw new IllegalStateException("Elasticsearch domain must have @Document annotation!");
         }
  
         Document annotation = clazz.getDeclaredAnnotation(Document.class);
         return annotation.indexName();
     }
  </code></pre>
  
  <p>So the result would be <code>email-2020-06</code>.</p>
  
  <p>Now, searching in this feels a little off:</p>
  
  <pre><code>    public &lt;T extends AbstractDocument&gt; T searchOneDated(Query query, Class&lt;T&gt; clazz) {
          String indexName = elasticSearchMappingInitializer.getIndexName(clazz);
          indexName = format("%s-*", indexName);
  
          return ofNullable(elasticsearchOperations.searchOne(query, clazz, IndexCoordinates.of(indexName)))
                  .map(SearchHit::getContent)
                  .orElse(null);
      }
  </code></pre>
  
  <p>I can't shake the feeling that there's a way to do this in a much simpler way, but like i said, i'm a novice, and the reference documentation for 4.0.0 did not help with this case, so i'd appreciate any input from someone who knows more about this. Thanks in advance. </p>