I want to write a query in Elastic that applies a filter based on values i have in an array (in my R program). Essentially the query:
I have the following primitive version of the query but do not know how to use the oth_usr array in a query (part 2 above).
query <- sprintf('{"query":{"range":{"time":{"gte":"%s","lte":"%s"}}}}',start_date,end_date)
view_list <- elastic::Search(index = "organised_recent",type = "PROPERTY_VIEW",size = 10000000,
body=query, fields = c("trackId", "propertyId"))$hits$hits
答案 0 :(得分:0)
您需要添加terms
个查询,并将range
一个查询嵌入到bool/must
查询中。尝试更新您的查询,如下所示:
terms <- paste(sprintf("\"%s\"", oth_usr), collapse=", ")
query <- sprintf('{"query":{"bool":{"must":[{"terms": {"trackId": [%s]}},{"range": {"time": {"gte": "%s","lte": "%s"}}}]}}}',terms,start_date,end_date)
答案 1 :(得分:0)
我不熟练使用R语法,但这是原始的JSON查询。
它会检查您的time
字段是否与给定范围(start_time
和end_time
)匹配,以及您的terms
完全匹配trackId
是否匹配。
根据您的要求,它仅返回trackId
,propertyId
个字段:
POST /indice/_search
{
"_source": {
"include": [
"trackId",
"propertyId"
]
},
"query": {
"bool": {
"must": [
{
"range": {
"time": {
"gte": "start_time",
"lte": "end_time"
}
}
},
{
"terms": {
"trackId": [
"terms"
]
}
}
]
}
}
}