0

I do not understand how to use searchTemplates in the Java API of Elasticsearch correctly. My template seems to work fine when I test it in sense. But when I use the template in Java code, it gives different results.

Here is what I do

DELETE /megacorp

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}


GET /megacorp/_search
{
  "query": {"match": {
    "about": "rock"
  }}
}

This returns:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.11506981,
    "hits": [
      {
        "_index": "megacorp",
        "_type": "employee",
        "_id": "1",
        "_score": 0.11506981,
        "_source": {
          "first_name": "John",
          "last_name": "Smith",
          "age": 25,
          "about": "I love to go rock climbing",
          "interests": [
            "sports",
            "music"
          ]
        }
      }
    ]
  }
}

So that looks nice: a score of 0.115. Now I create a searchTemplate

DELETE /_search/template/megacorpTemplate

POST /_search/template/megacorpTemplate
{
  "template": {
    "query": {
      "match": {
        "about": "{{txt}}"
      }
    }
  }
}

And use it:

GET /megacorp/_search/template
{
    "id": "megacorpTemplate", 
    "params": {
        "txt":  "rock" 
    }
}

It returns:

{
  "took": 35,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.11506981,
    "hits": [
      {
        "_index": "megacorp",
        "_type": "employee",
        "_id": "1",
        "_score": 0.11506981,
        "_source": {
          "first_name": "John",
          "last_name": "Smith",
          "age": 25,
          "about": "I love to go rock climbing",
          "interests": [
            "sports",
            "music"
          ]
        }
      }
    ]
  }
}

So, all good and well. But now comes the problem. When I want to use this searchTemplate in my Java code, I seem to lose some information, for example the score is 1.0 and my script fields are lost (removed for brevity in this sample). Here is my code:

   @Test
   public void quickTest2() {
       Client client;
        try {
            client = TransportClient.builder().build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));


               Map<String,Object> templateParams = new HashMap<>();
               templateParams.put("txt", "rock");

               QueryBuilder tqb = QueryBuilders.templateQuery(
                        "megacorpTemplate",                  
                        ScriptService.ScriptType.INDEXED,    
                        templateParams);  

               SearchResponse searchResponse = client.prepareSearch("megacorp")
                       .setQuery(tqb)
                       .execute()
                       .actionGet(); 

               System.out.println(searchResponse.toString());


        } 
        catch (UnknownHostException e) {
            e.printStackTrace();
        }
   }

It returns:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 1.0,
      "_source" : {
        "first_name" : "John",
        "last_name" : "Smith",
        "age" : 25,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

So why is my score 1.0 now instead of 0.115?

2
  • If you add .setExplain(true) to your prepareSearch, what does it say? I'm guess that it is doing a constantscore query. I also think you want to 'PUT' to ' /_search/template/megacorpTemplate' instead of 'POST' Commented Oct 19, 2016 at 19:40
  • Yes, it is doing a constantscore query Commented Oct 21, 2016 at 14:26

1 Answer 1

1

For those who are interested, using a Template and the setTemplate method solved my problem.

   @Test
   public void quickTest2() {
       Client client;
        try {
            client = TransportClient.builder().build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

               Map<String,Object> templateParams = new HashMap<>();
               templateParams.put("txt", "rock");

               Template template = new Template("megacorpTemplate", ScriptService.ScriptType.INDEXED, MustacheScriptEngineService.NAME, null, templateParams);
               SearchRequestBuilder request = client.prepareSearch("megacorp").setTemplate(template);
               SearchResponse searchResponse = request.execute().actionGet();

               System.out.println(searchResponse.toString());
        } 
        catch (UnknownHostException e) {
            e.printStackTrace();
        }
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.