【问题标题】:What is wrong with this elastic json query, mapping?这个弹性 json 查询、映射有什么问题?
【发布时间】:2020-07-02 21:19:06
【问题描述】:

我正在尝试使用嵌套 JSON 来查询数据库记录。这是我的查询-

"query": {
"nested": {
  "path": "metadata.technical",
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "metadata.technical.key": "techcolor"
          }
        },
        {
          "term": {
            "metadata.technical.value": "red"
          }
        }
      ]
    }
  }
}

}

这是我的 mapping.json 中的这一部分 -

"metadata": {
      "include_in_parent": true,
      "properties": {
        "technical": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "string"
            },
            "value": {
              "type": "string"
            }
          }
        }
      }
    }

我有一个包含“值”列的表格,它的内容是 -

{"technical":
    {
     "techname22": "test",
     "techcolor":"red",
     "techlocation": "usa"
    }
}

为什么我不能得到任何结果?仅供参考,我使用的是 ES 1.7。感谢您的帮助。

【问题讨论】:

    标签: json elasticsearch


    【解决方案1】:

    为了尊重您定义的映射,您的示例文档应如下所示:

    {
      "technical": [
        {
          "key": "techname22",
          "value": "test"
        },
        {
          "key": "techcolor",
          "value": "red"
        },
        {
          "key": "techlocation",
          "value": "usa"
        }
      ]
    }
    

    使用上述结构更改您的文档将使您的查询按原样工作。

    本文档的真实映射:

    {
      "technical": {
        "techname22": "test",
        "techcolor": "red",
        "techlocation": "usa"
      }
    }
    

    更像是这样:

    {
      "include_in_parent": true,
      "properties": {
        "technical": {
          "type": "nested",
          "properties": {
            "techname22": {
              "type": "string"
            },
            "techcolor": {
              "type": "string"
            },
            "techlocation": {
              "type": "string"
            }
          }
        }
      }
    }
    

    如果您的所有键都是动态的并且事先不知道,您也可以将映射配置为动态的,即不要在嵌套类型中定义任何字段,如果新字段尚未出现在映射:

    {
      "include_in_parent": true,
      "properties": {
        "technical": {
          "type": "nested",
          "properties": {
          }
        }
      }
    }
    

    【讨论】:

    • 好的。那么如果技术关键和价值是动态的,我应该改变什么?并且能够通过技术属性中的特定键和值进行搜索?
    • 嗯。但是我可以让它与这样的文件一起使用吗?
    • 您需要更改查询以使用实际的字段名称
    • 虽然这个配置不起作用 - pastebin.com/0uVsFgGw 它什么也没找到
    • 我需要如何更改此类文档的查询?
    猜你喜欢
    • 2020-10-07
    • 2011-04-16
    • 1970-01-01
    • 2012-12-28
    • 2014-08-22
    • 2011-09-29
    • 2011-10-13
    相关资源
    最近更新 更多