2016-10-19 13 views
0

ElasticsearchのJava APIでsearchTemplatesを正しく使用する方法がわかりません。私のテンプレートは意味をなさないテストでうまく動作しているようです。しかし、テンプレートをJavaコードで使用すると、結果が異なります。Java APIでelasticsearch検索テンプレートを使用

{ 
    "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のスコアをここで

は、私はこれが返す

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

を行うものです。今、私は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'に追加すると、それは何と言いますか?私はそれが定数スコアクエリを行っていると思います。また、 'POST'の代わりに '/ _search/template/megacorpTemplate'に 'PUT'したいと思っています – Phil

+0

はい、定数スコアクエリを実行しています – Willem

答えて

0

興味のある方は、テンプレートと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(); 
     } 
    } 
関連する問題