简体   繁体   English

在Java API中使用elasticsearch搜索模板

[英]using elasticsearch searchtemplates in Java API

I do not understand how to use searchTemplates in the Java API of Elasticsearch correctly. 我不明白如何在Elasticsearch的Java API中正确使用searchTemplates。 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. 但是当我在Java代码中使用模板时,它会给出不同的结果。

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. 这样看起来很不错:分数为0.115。 Now I create a searchTemplate 现在我创建一个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). 当我想在Java代码中使用此searchTemplate时,似乎丢失了一些信息,例如,得分为1.0,并且我的脚本字段也丢失了(为简洁起见,已将其删除)。 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? 那么,为什么我的分数现在是1.0,而不是0.115?

For those who are interested, using a Template and the setTemplate method solved my problem. 对于那些感兴趣的人,使用Template和setTemplate方法解决了我的问题。

   @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();
        }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM