【问题标题】:Elasticsearch search with nested objects使用嵌套对象进行 Elasticsearch 搜索
【发布时间】:2021-06-17 16:01:36
【问题描述】:

我在弹性中有以下对象。

有没有办法运行全文搜索查询,例如:Fizz AND bar

{
  "258": "Fizz buzz",
  "12416": [
    {
      "161": 54,
      "273": "text doc.txt",
      "544": [
        "text/plain"
      ],
      "12290": "foo bar",
    }
  ]
}

我已经阅读了有关运行时字段的信息,但它仅支持关键字类型,并且仅在完全匹配的情况下才有效。

【问题讨论】:

  • 您要在哪个字段上运行查询?
  • 有没有办法让它同时适用于 258 和 12416
  • 12416 是嵌套对象还是数组?你想查询 12416 的哪个字段?
  • 查询中不能设置特定字段的问题。

标签: elasticsearch


【解决方案1】:

查询字符串不适用于嵌套字段。您可以使用嵌套查询或实现自定义_all field

PUT index117
{
  
  "mappings": {
    "properties": {
      "_all":{
        "type": "text"
      },
      "258":{
        "type": "text",
        "copy_to": "_all"  --> copies data to _all field
      },
      "12416":{
        "type": "nested",
        "properties": {
          "12290":{
            "type":"text",
            "copy_to": "_all"
          }
        }
      }
    }
  }
}


POST index117/_doc
{
  "258": "Fizz buzz",
  "12416": [
    {
      "161": 54,
      "273": "text doc.txt",
      "544": [
        "text/plain"
      ],
      "12290": "foo bar"
    }
  ]
}

GET index117/_search
{
  "query": {
    "query_string": {
      "default_field": "_all",
      "query": "foo AND bar"
    }
  }  
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2016-03-27
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多