2016-11-24 83 views
0

首先,我开始连接:使用模板来查询ElasticSearch有R

connect(es_host = "172.19.28.5") 

,然后搜索我的模板(模板名称的getKpiHistMetric):

Search_template_get('getKpiHistMetric') 

,其结果是:

>$lang 
[1] "mustache" 

$`_id` 
[1] "getKpiHistMetric" 

$found 
[1] TRUE 

$`_version` 
[1] 2 

$template 
[1] "{"size": 1000, 
    "query": { 
     "constant_score": { 
     "filter": { 
      "bool": { 
      "must": [ 
       {"term":{"KpiKey":"{{KpiKey}}"}}, 
       {"range":{"HistWriteTimestamp":{ 
              "from":"{{from}}", 
              "to":"{{to}}" 
              } 
         } 
       }] 
    }}}}}" 

因此,我可以到达服务器并找到模板查询。该模板有三个参数:KpiKey,fromto。如何使用此模板查询数据库?什么功能被使用?我如何传递参数?

谢谢。

回答

1

重复的例子

library(elastic) 
conntect() 

负载iris数据集中到ES

if (!index_exists("iris")) { 
    invisible(docs_bulk(iris, "iris")) 
} 

做一个模板

body <- '{ 
    "template": { 
    "query": { 
     "match": { 
      "Species": "{{query_string}}" 
     } 
    } 
    } 
}' 

注册模板

Search_template_register(template = 'foobar', body = body) 

定义搜索,使用模板名称作为Search_template_register定义,

body2 <- '{ 
"id": "foobar", 
    "params": { 
     "query_string": "setosa" 
    } 
}' 

搜索模板,Search_template

Search_template(body = body2) 
  • 与Elaticsearch v5.0.0,R v3.3.2
+0

谢谢为了您的答案,我会接受它。你能指点一下如何在'body'和'body2'变量中创建代码吗? – Eduardo

+0

正文可以是R列表或JSON,'elastic'文档有每个例子。有关示例,请参阅Elastic文档https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-template.html – sckott