简体   繁体   English

Elasticsearch 查询未给出预期结果

[英]Elasticsearch query not giving expected result

This is the doc I have in my index(there can be several too):这是我索引中的文档(也可以有几个):

{
        "_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"
        }
      }

Logically, I am trying to build this condItion:从逻辑上讲,我正在尝试建立这种条件:

if((projectId==62 && startDate>=inputFrom && startDate <=inputTo) && (status==ACTIVE ||status==POSTPONED))

My Query(from kibana):我的查询(来自 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
  }
  }
}

I am comparing with range query for startDate field within mentioned range with other must fields above.我正在将上述范围内的startDate字段的范围查询与上面的其他must字段进行比较。 But not getting any hit!但没有受到任何打击! I want to retrieve documents which has startDate within the range of given from and to date.我想检索startDate在给定fromto date 范围内的文档。
Being quite inexperienced in this arena, don't know why not working?在这个领域相当缺乏经验,不知道为什么不工作? Please help how to fix this query to do that?请帮助如何修复此查询来做到这一点?

You can use bool to combine multiple queries in this way.您可以使用 bool 以这种方式组合多个查询。 The must clause will work as logical AND, and will make sure all the conditions are matched. must 子句将用作逻辑 AND,并确保所有条件都匹配。

To return documents that contain terms within a provided range refer this ES official documentation on Range Query要返回包含指定范围内术语的文档,请参阅有关Range Query的 ES 官方文档

Since you have not provided any mapping for the data you have indexed, I have not specified any explicit mapping for the index.由于您没有为已索引的数据提供任何映射,因此我没有为索引指定任何显式映射。

The below search query will give you the expected result according to your condition specified in the question.以下搜索查询将根据您在问题中指定的条件为您提供预期结果。

Search Query:搜索查询:

{
  "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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM