2015-07-21 81 views
1

我一直在ES部分中的這個站點Parameterizing Queries in Solr and Elasticsearch的示例。請注意,它是作者正在使用的較舊的ES版本,但我認爲這不會影響這種情況。我正在使用ES的1.6版本。Elasticsearch模板不能讀取參數

我有一個模板{{ES_HOME}}/config/scripts/test.mustache,看起來像下面的代碼片段。請注意「查詢」中的{{q}}參數。

{ 
    "query": { 
    "multi_match": { 
     "query": "{{q}}", 
     "analyzer": "keyword", 
     "fields": [ 
      "description^10", 
      "name^50", 
     ] 
    } 
    }, 
    "aggregations": { 
    "doctype" : { 
     "terms" : { 
     "field" : "doctype.untouched"  
     } 
    } 
    } 
} 

將帖子發到http://localhost:9200/forward/_search/template以下消息體

{ 
    "template": { 
    "file": "test", 
    "params": { 
     "q": "a" 
    } 
    } 
} 

它運行的模板,但得到0命中,並返回如下:

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 0, 
     "max_score": null, 
     "hits": [] 
    }, 
    "aggregations": { 
     "doctype": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [] 
     }, 
    } 
} 

或者,如果我硬將代碼「a」代入{{q}}中,然後發送到模板,它會正確返回查詢「a」的結果。我在設計中做了一些固有的錯誤嗎?

回答

1

根據該docs,將params對象應該是外template對象

{ 
    "template": { 
     "file": "test" 
    }, 
    "params": { 
     "q": "a" 
    } 
} 
+0

衛生署的。有些例子中,模板對象中有文件和參數,但只有當模板對象位於查詢對象內部時纔有效。這解決了它 – Shark