【发布时间】:2016-02-28 02:42:48
【问题描述】:
我需要一些帮助来重写我的 elasticsearch 查询。
我需要的是:
1- 如果verb 和sessionid.raw 两个字段完全匹配,则显示一条记录(不接受部分匹配)。
"must": [
{ "match" : { "verb" : "login" } },
{ "term" : { "sessionid.raw" : strSessionID } },
]
或
2- 显示与其他一些字段匹配的前 5 条记录(按_score DESC 和@timestamp ASC 排序),如果记录在指定时间范围内,则给予提升。
"must": [
{ "match" : { "verb" : "login" } },
{ "term" : { "pid" : strPID } },
],
"should": [
{ "match" : { "user.raw" : strUser } },
{ "range" : { "@timestamp" : {
"from" : QueryFrom,
"to" : QueryTo,
"format" : DateFormatElastic,
"time_zone" : "America/Sao_Paulo",
"boost" : 2 }
} },
]
下面的代码几乎是在做我想要的。
现在如果找到它会将sessionid.raw 提升到顶部,但不会丢弃剩余的记录。
var objQueryy = {
"fields" : [ "@timestamp", "program", "pid", "sessionid.raw", "user", "frontendip", "frontendname", "_score" ],
"size" : ItemsPerPage,
"sort" : [ { "_score" : { "order": "desc" } }, { "@timestamp" : { "order" : "asc" } } ],
"query" : {
"bool": {
"must": [
{ "match" : { "verb" : "login" } },
{ "term" : { "pid" : strPID } },
{ "bool": {
"should": [
{ "match" : { "user.raw" : strUser } },
{ "match" : { "sessionid.raw": { "query": strSessionID, "boost" : 3 } } },
{ "range" : { "@timestamp" : { "from": QueryFrom, "to": QueryTo, "format": DateFormatElastic, "time_zone": "America/Sao_Paulo" } } },
],
}},
],
},
},
}
【问题讨论】:
-
你想让
should clause里面的三个条件都匹配吗? -
@ChintanShah25 不是真的。只有
must子句中的那些是强制性的。 -
好的,如果您能向我们展示一些您认为应该丢弃但仍然显示的示例文档,那就太好了
标签: json search elasticsearch lucene full-text-search