<p dir="auto">If you try to sort by "fieldname", and "fieldname" is the name of a mapped field as specified by "index_name", you get parse error stating that no mapping was found for that field. It doesn't seem right that you can search by the index_name but not sort by it, hence this bug. Without a fix for this I may be forced to put my data in the document twice, so that I can have an actual field with the correct name instead of using index_name.</p>
<p dir="auto">Discussion on the forum:<br>
<a href="https://groups.google.com/forum/#!topic/elasticsearch/6-BWdQTPTH0" rel="nofollow">https://groups.google.com/forum/#!topic/elasticsearch/6-BWdQTPTH0</a></p>
<p dir="auto">Reproduced in 1.4 via this gist:<br>
<a href="https://gist.github.com/pmishev/11375297">https://gist.github.com/pmishev/11375297</a></p>
<p dir="auto">I have the following problem writing an own query parser from my own package:</p>
<p dir="auto">My query parser has to internally create another Elasticsaearch query and parse it (which is actually a workaround: I need to create a MoreLikeThis query, but I dont want to replicate all code to generate the native Lucene Query). So I use MoreLikeThisQueryBuilder to create a BytesReference of the newly constructed query, so I can parse it to a Lucene Query.</p>
<p dir="auto">Of course I cannot do the parseContext.parseInnerQuery, because the newly constructed query is in a separate BytesReference. Instead I do the same trick like WrappedQuery:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    final BytesReference querySource = QueryBuilders.moreLikeThisQuery(FIELDNAME_MLT)
      .ids(Integer.toString(id))
      .include(true)
      .percentTermsToMatch(.5f)
      .minTermFreq(0)
      .minDocFreq(5)
      .maxQueryTerms(25)
      .boostTerms(1.0f)
      .buildAsBytes();
    // hack for parsing this as inner query
    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) {
      final QueryParseContext context = cloneParseContext();
      context.reset(qSourceParser);
      return context.parseInnerQuery();
    } catch (IOException ioe) {
      throw new AssertionError(&quot;IOException cannot happen here.&quot;, ioe);
    }"><pre class="notranslate"><code class="notranslate">    final BytesReference querySource = QueryBuilders.moreLikeThisQuery(FIELDNAME_MLT)
      .ids(Integer.toString(id))
      .include(true)
      .percentTermsToMatch(.5f)
      .minTermFreq(0)
      .minDocFreq(5)
      .maxQueryTerms(25)
      .boostTerms(1.0f)
      .buildAsBytes();
    // hack for parsing this as inner query
    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) {
      final QueryParseContext context = cloneParseContext();
      context.reset(qSourceParser);
      return context.parseInnerQuery();
    } catch (IOException ioe) {
      throw new AssertionError("IOException cannot happen here.", ioe);
    }
</code></pre></div>
<p dir="auto">The problem is now: I need to clone the QueryParseContext. WrappedQueryParser just creates a new instance, but it needs the index name (easy to get) and IndexQueryParserService. Unfortunately, the latter is package private.</p>
<p dir="auto">I would be happy if this could be solved. As this above stuff is used quite often, I think the best solution would be to allow to clone ParseContext or add a method to create a new one from an existion one (copy constructor). The other way would be to make the field accessible.</p>
<p dir="auto">The current workaround is to use reflection to get the field...</p>