OAK-9311 oak-search-elastic: improved low-level request creation

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1885401 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticRequestHandler.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticRequestHandler.java
index b57b89e..b560ca9 100644
--- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticRequestHandler.java
+++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticRequestHandler.java
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.elastic.query;
 
+import org.apache.http.entity.ContentType;
+import org.apache.http.nio.entity.NByteArrayEntity;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
@@ -44,8 +46,10 @@
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.lucene.search.WildcardQuery;
 import org.apache.lucene.search.join.ScoreMode;
+import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.client.Request;
-import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.xcontent.XContentHelper;
+import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.index.query.BoolQueryBuilder;
 import org.elasticsearch.index.query.InnerHitBuilder;
 import org.elasticsearch.index.query.MatchBoolPrefixQueryBuilder;
@@ -77,6 +81,7 @@
 
 import javax.jcr.PropertyType;
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -110,7 +115,6 @@
 import static org.apache.jackrabbit.oak.spi.query.QueryConstants.JCR_SCORE;
 import static org.apache.jackrabbit.util.ISO8601.parse;
 import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
-import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 import static org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item;
 import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
 import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
@@ -266,16 +270,21 @@
      * Receives a {@link SearchSourceBuilder} as input and converts it to a low level {@link Request} reducing the response
      * in order to reduce size and improve speed.
      * https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-response-filtering
+     *
      * @param searchSourceBuilder the search request
-     * @param indexName the index to query
+     * @param indexName           the index to query
      * @return a low level {@link Request} instance
      */
     public Request createLowLevelRequest(SearchSourceBuilder searchSourceBuilder, String indexName) {
         String endpoint = "/" + indexName
-                + "/_search?filter_path=took,timed_out,hits.total.value,hits.hits._score,hits.hits.sort,,hits.hits._source,aggregations";
+                + "/_search?filter_path=took,timed_out,hits.total.value,hits.hits._score,hits.hits.sort,hits.hits._source,aggregations";
         Request request = new Request("POST", endpoint);
         try {
-            request.setJsonEntity(Strings.toString(searchSourceBuilder.toXContent(jsonBuilder(), EMPTY_PARAMS)));
+            BytesRef source = XContentHelper.toXContent(searchSourceBuilder, XContentType.JSON, EMPTY_PARAMS, false).toBytesRef();
+            request.setEntity(
+                    new NByteArrayEntity(source.bytes, source.offset, source.length,
+                            ContentType.create(XContentType.JSON.mediaTypeWithoutParameters(), (Charset) null))
+            );
         } catch (IOException e) {
             throw new IllegalStateException("Error creating request entity", e);
         }
diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticResponseHandler.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticResponseHandler.java
index 1513922..8cb54c6 100644
--- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticResponseHandler.java
+++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/ElasticResponseHandler.java
@@ -67,24 +67,18 @@
 
     public String getPath(SearchHit hit) {
         Map<String, Object> sourceMap = hit.getSourceAsMap();
-        return  transformPath((String) sourceMap.get(FieldNames.PATH));
+        return transformPath((String) sourceMap.get(FieldNames.PATH));
     }
 
     private String transformPath(String path) {
-        if ("".equals(path)) {
-            path = "/";
-        }
-        if (planResult.isPathTransformed()) {
-            String originalPath = path;
-            path = planResult.transformPath(path);
+        String transformedPath = planResult.transformPath(("".equals(path)) ? "/" : path);
 
-            if (path == null) {
-                LOG.trace("Ignoring path {} : Transformation returned null", originalPath);
-                return null;
-            }
+        if (transformedPath == null) {
+            LOG.trace("Ignoring path {} : Transformation returned null", path);
+            return null;
         }
 
-        return path;
+        return transformedPath;
     }
 
     public boolean isAccessible(String path) {
diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticPerfTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticPerfTest.java
index 3af557e..c81412f 100644
--- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticPerfTest.java
+++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticPerfTest.java
@@ -29,7 +29,6 @@
 
 import java.util.Iterator;
 import java.util.Random;
-import java.util.UUID;
 import java.util.function.Supplier;
 
 import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
@@ -68,7 +67,7 @@
         builder.indexRule("nt:base").property(PROP_3).analyzed();
         builder.indexRule("nt:base").property(PROP_1).analyzed();
 
-        this.index = setIndex(UUID.randomUUID().toString(), builder);
+        this.index = setIndex("" + System.currentTimeMillis(), builder);
         root.commit();
     }
 
@@ -140,7 +139,7 @@
     }
 
     private void createTestData(Supplier<String> extraContentSupplier) throws Exception {
-        long start = LOG_PERF.start("Starting data indexing");
+        long start = LOG_PERF.startForInfoLog("Starting data indexing");
         Tree content = root.getTree("/").addChild("content");
 
         for (int i = 0; i < NUM_SUB_CONTENT; i++) {
@@ -163,20 +162,20 @@
         assertEventually(() ->
                 assertThat(countDocuments(index), equalTo((long) ((NUM_SUB_CONTENT * NUM_NODES) + NUM_SUB_CONTENT)))
         );
-        LOG_PERF.end(start, -1, "{} documents indexed", countDocuments(index));
+        LOG_PERF.end(start, -1,-1, "{} documents indexed", countDocuments(index));
     }
 
     private void testQuery(String query, String language) throws Exception {
         Result result = executeQuery(query, language, NO_BINDINGS);
         Iterable<ResultRow> it = (Iterable<ResultRow>) result.getRows();
         Iterator<ResultRow> iterator = it.iterator();
-        long start = LOG_PERF.start("Getting result rows");
+        long start = LOG_PERF.startForInfoLog("Getting result rows");
         int i = 0;
         while (iterator.hasNext()) {
             ResultRow row = iterator.next();
             i++;
         }
-        LOG_PERF.end(start, -1, "{} Results fetched", i);
+        LOG_PERF.end(start, -1,-1, "{} Results fetched", i);
     }
 
 }