Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-12034

camel-elasticsearch5 - Search Operation: If Map or String is used in Message Body, "size" and "from" parameters are always ignored

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.20.1
    • Fix Version/s: 2.20.3, 2.21.0
    • Component/s: camel-elasticsearch5
    • Labels:
      None
    • Estimated Complexity:
      Unknown

      Description

      Hi,

      I am using camel-elasticsearch5 component of Camel 2.20.1.
      I have found and issue. The description follows.

      If you use Map or String in message body for SEARCH operation, "size" and "from" parameters are always ignored hence you always get just default 10 results.

      For example - if your map contains query like this:
      (in terms of simplicity - following is String representation of the map):

      {size=50, query={query_string={query=status:ACTIVE}}, from=0}
      

      Issue I suspect is present in class: org.apache.camel.component.elasticsearch5.converter.ElasticsearchActionRequestConverter
      and its method
      public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange)

      // line 191...
      if (queryObject instanceof Map<?, ?>) {
                  Map<String, Object> mapQuery = (Map<String, Object>)queryObject;
                  // Remove 'query' prefix from the query object for backward compatibility
                  if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
                      mapQuery = (Map<String, Object>)mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
                  }
                  try {
                      XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
                      queryText = contentBuilder.map(mapQuery).string();
                  } catch (IOException e) {
                      LOG.error(e.getMessage());
                  }
              }
      
      // queryText then used on line 220...
      searchSourceBuilder.query(QueryBuilders.wrapperQuery(queryText));
      searchRequest.source(searchSourceBuilder);
      
      return searchRequest;
      

      Inner if condition basically extracts only query part from the map

      query={query_string={query=status:ACTIVE}
      

      and "size" and "from" get lost from the query

      // line 194...
      if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
                      mapQuery = (Map<String, Object>)mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
      // ...where ElasticsearchConstants.ES_QUERY_DSL_PREFIX = "query"
      }
      

      Same issue is with usage of the String in the message body:

      // line 203
      } else if (queryObject instanceof String) {
                  queryText = (String)queryObject;
                  ObjectMapper mapper = new ObjectMapper();
                  try {
                      JsonNode jsonTextObject = mapper.readValue(queryText, JsonNode.class);
                      JsonNode parentJsonNode = jsonTextObject.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
                      if (parentJsonNode != null) {
                          queryText = parentJsonNode.toString();
                      }
                  } catch (IOException e) {
                      LOG.error(e.getMessage());
                  }
              }
      

      Only workaround for this is to use SearchRequest object in a message body where you can explicitly set "size" and "from" on SearchSourceBuilder object. For example:

      SearchRequest searchRequest = new SearchRequest("my_index")
                  .types("my_type")
                  .source(new SearchSourceBuilder().query(QueryBuilders.wrapperQuery("my:query"))
                      .size(50).from(0));
      

      I don't know what was the developer's intention for having such a condition which removes these parameters from the query.

      Thank you very much in advance if anybody can have a look and verify if this is a valid concern.

        Attachments

          Activity

            People

            • Assignee:
              davsclaus Claus Ibsen
              Reporter:
              Radoslav.Cincala Radoslav Cincala
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: