【问题标题】:ElasticSearch query doesn't return documents that have an "empty" nested propertyElasticSearch 查询不返回具有“空”嵌套属性的文档
【发布时间】:2017-09-16 01:43:01
【问题描述】:

我遇到了一个奇怪的问题。我有一个文档映射,其中一个属性是嵌套对象。

{
    "userLog": {
        "properties": {
            "userInfo": {
                "userId": {
                    "type": "text"
                },
                "firstName": {
                    "type": "text"
                },
                "lastName": {
                    "type": "text"
                },
                "email": {
                    "type": "text"
                }
            },
            "violations": {
                "type": "integer"
            },
            "malfunctions": {
                "type": "integer"
            },
            "extensionsUsed": {
                "type": "integer"
            },
            "date": {
                "type": "date",
                "format": "yyyy-MM-dd||yyyy/MM/dd||yyyyMMdd||epoch_millis"
            },
            "events": {
                "type": "nested",
                "properties": {
                    "editorId": {
                        "type": "text"
                    },
                    "editorRole": {
                        "type": "text"
                    },
                    "editedTimestamp": {
                        "type": "date",
                        "format": "epoch_millis"
                    },
                    "createdTimestamp": {
                        "type": "date",
                        "format": "epoch_millis"
                    },
                    "userId": {
                        "type": "text"
                    },
                    "timestamp": {
                        "type": "date",
                        "format": "epoch_millis"
                    },
                    "eventType": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

有些用户日志有事件,有些没有。但是,我的查询只返回有事件的用户日志,我不知道为什么。索引中肯定存在没有事件的用户日志。我可以在 Kibana 中看到它们。他们只是没有在搜索中返回。这是我正在运行的查询:

GET index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "gte": "20170913",
              "format": "yyyyMMdd"
            }
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "userInfo.userId": "Xvo9qblajOVaM3bQQMaV4GKk7S42"
          }
        }
      ],
      "minimum_number_should_match": 1
    }
  }
}

基于this discussion

我将查询修改为以下内容:

GET one20_eld_portal/_search
{
    "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "events",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "events.userId"
                  }
                }
              }
            }
          }
        }
      ],
        "should": [
          {
            "match_phrase": {
              "userInfo.uid": "Xvo9qblajOVaM3bQQMaV4GKk7S42"
            }
          }
        ],
        "minimum_should_match": 1
      }
  }
}

但这不会返回 任何 结果。非常感谢任何帮助!

【问题讨论】:

  • 您发布了两个查询。根据我收集的信息,第一个查询正在运行,第二个没有?如果我错了,请纠正我..
  • @Richa 我只是展示了我到目前为止所尝试的内容。第二个查询不返回任何结果,但第一个只返回非空事件的结果
  • 还有一个问题.. 文档应该满足什么标准?根据第一个查询date 应该大于给定值并且 userInfo.userId 应该是Xvo9qblajOVaM3bQQMaV4GKk7S42。而第二个查询说 events.userId 不能存在并且 userInfo.userId 应该是Xvo9qblajOVaM3bQQMaV4GKk7S42。两者是不同的。能不能简单的解释一下。我们将尝试将其转换为弹性查询
  • @Richa 我发现了问题,原来创建 userLog 的机制没有正确设置 userInfo.userId。我的疏忽很大,感谢您的帮助!

标签: elasticsearch elasticsearch-5


【解决方案1】:

原来没有返回“空”日志的原因是没有为空日志正确设置 userId。

【讨论】:

    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多