2016-10-19 79 views
0

我不明白如何在Elasticsearch的Java API中正確使用searchTemplates。當我測試它時,我的模板似乎工作正常。但是當我在Java代碼中使用模板時,它會給出不同的結果。在Java API中使用elasticsearch searchtemplates

這是我做的

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" 
    }} 
} 

這將返回:

{ 
    "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" 
      ] 
     } 
     } 
    ] 
    } 
} 

所以這看起來不錯:得分爲0.115。現在,我創建了一個searchTemplate

DELETE /_search/template/megacorpTemplate 

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

並使用它:

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

它返回:

{ 
    "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" 
      ] 
     } 
     } 
    ] 
    } 
} 

所以,所有的好,好。但現在出現了這個問題。當我想在我的Java代碼中使用這個searchTemplate時,我似乎失去了一些信息,例如分數是1.0,並且我的腳本字段丟失(爲簡潔起見,在本示例中)。這裏是我的代碼:

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

它返回:

{ 
    "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" ] 
     } 
    } ] 
    } 
} 

那麼,爲什麼我的分數1.0,而不是現在的0.115?

+0

如果你將'.setExplain(true)'添加到'prepareSearch'中,它說什麼?我猜測它正在做一個constantscore查詢。我也認爲你想''放'到'/ _search/template/megacorpTemplate'而不是'POST' – Phil

+0

是的,它正在做一個constantscore查詢 – Willem

回答

0

對於那些有興趣的人來說,使用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(); 
     } 
    } 
相關問題