Elasticsearch 查询未给出预期结果
Elasticsearch query not giving expected result
这是我索引中的文档(也可以有多个):
{
"_index" : "meeting",
"_type" : "meeting",
"_id" : "27",
"_score" : 1.0,
"_source" : {
"createTime" : "2020-07-20T14:49:05.803",
"createTimeInMs" : 1595234945803,
"createdBy" : "sdf@sdfsd.com",
"editTime" : "2020-07-20T14:49:05.803",
"editTimeInMs" : 1595234945803,
"editedBy" : "sfsf@sdfsdf.com",
"versionId" : 1,
"id" : "27",
"projectId" : "62",
"projectName" : "sdfsdf",
"meetingName" : "meeting1",
"agenda" : "string",
"startDateString" : "2020-07-20T00:45:19.604",
"userId" : 3,
"memberList" : [
3,
4
],
"status" : "ACTIVE",
"timeZone" : "Asia/Dhaka",
"startDate" : "2020-07-20T00:45:19.604",
"endDate" : "2020-07-20T01:45:19.604",
"dateInMS" : 1595227519000,
"meetingPlace" : "string",
"endDateString" : "2020-07-20T01:45:19.604"
}
}
逻辑上,我试图建立这个条件:
if((projectId==62 && startDate>=inputFrom && startDate <=inputTo) && (status==ACTIVE ||status==POSTPONED))
我的查询(来自 kibana):
GET /meeting/_search?pretty
{
"query":
{
"bool" : {
"must" : [
{
"term" : {
"projectId" : {
"value" : "62",
"boost" : 1.0
}
}
},
{
"terms" : {
"status" : [
"ACTIVE",
"POSTPONED"
],
"boost" : 1.0
}
},
{
"range" : {
"startDate" : {
"from" : "2020-07-19T23:45:19.604Z",
"to" : "2020-07-20T00:49:19.604Z",
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}
我正在将上述范围内的 startDate
字段的范围查询与上述其他 must
字段进行比较。但是没有受到任何打击!
我想检索在给定 from
和 to
日期范围内具有 startDate
的文档。
在这个领域相当缺乏经验,不知道为什么不工作!请帮忙解决这个问题?
您可以使用 bool 以这种方式组合多个查询。 must 子句将用作逻辑 AND,并确保所有条件都匹配。
对于return 包含规定范围内术语的文档,请参阅 Range Query
上的 ES 官方文档
由于您没有为索引数据提供任何映射,因此我没有为索引指定任何显式映射。
下面的搜索查询将根据您在问题中指定的条件为您提供预期的结果。
搜索查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"projectId": "62"
}
},
{
"range": {
"startDate": {
"gte": "2020-07-20T00:44:19.604",
"lte": "2020-07-21T00:45:19.604"
}
}
},
{
"bool": {
"should": [
{
"term": {
"status.keyword": "ACTIVE"
}
},
{
"term": {
"status.keyword": "POSTPONED"
}
}
]
}
}
]
}
}
}
这是我索引中的文档(也可以有多个):
{
"_index" : "meeting",
"_type" : "meeting",
"_id" : "27",
"_score" : 1.0,
"_source" : {
"createTime" : "2020-07-20T14:49:05.803",
"createTimeInMs" : 1595234945803,
"createdBy" : "sdf@sdfsd.com",
"editTime" : "2020-07-20T14:49:05.803",
"editTimeInMs" : 1595234945803,
"editedBy" : "sfsf@sdfsdf.com",
"versionId" : 1,
"id" : "27",
"projectId" : "62",
"projectName" : "sdfsdf",
"meetingName" : "meeting1",
"agenda" : "string",
"startDateString" : "2020-07-20T00:45:19.604",
"userId" : 3,
"memberList" : [
3,
4
],
"status" : "ACTIVE",
"timeZone" : "Asia/Dhaka",
"startDate" : "2020-07-20T00:45:19.604",
"endDate" : "2020-07-20T01:45:19.604",
"dateInMS" : 1595227519000,
"meetingPlace" : "string",
"endDateString" : "2020-07-20T01:45:19.604"
}
}
逻辑上,我试图建立这个条件:
if((projectId==62 && startDate>=inputFrom && startDate <=inputTo) && (status==ACTIVE ||status==POSTPONED))
我的查询(来自 kibana):
GET /meeting/_search?pretty
{
"query":
{
"bool" : {
"must" : [
{
"term" : {
"projectId" : {
"value" : "62",
"boost" : 1.0
}
}
},
{
"terms" : {
"status" : [
"ACTIVE",
"POSTPONED"
],
"boost" : 1.0
}
},
{
"range" : {
"startDate" : {
"from" : "2020-07-19T23:45:19.604Z",
"to" : "2020-07-20T00:49:19.604Z",
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}
我正在将上述范围内的 startDate
字段的范围查询与上述其他 must
字段进行比较。但是没有受到任何打击!
我想检索在给定 from
和 to
日期范围内具有 startDate
的文档。
在这个领域相当缺乏经验,不知道为什么不工作!请帮忙解决这个问题?
您可以使用 bool 以这种方式组合多个查询。 must 子句将用作逻辑 AND,并确保所有条件都匹配。
对于return 包含规定范围内术语的文档,请参阅 Range Query
上的 ES 官方文档由于您没有为索引数据提供任何映射,因此我没有为索引指定任何显式映射。
下面的搜索查询将根据您在问题中指定的条件为您提供预期的结果。
搜索查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"projectId": "62"
}
},
{
"range": {
"startDate": {
"gte": "2020-07-20T00:44:19.604",
"lte": "2020-07-21T00:45:19.604"
}
}
},
{
"bool": {
"should": [
{
"term": {
"status.keyword": "ACTIVE"
}
},
{
"term": {
"status.keyword": "POSTPONED"
}
}
]
}
}
]
}
}
}