elasticsearch - 如何创建具有多类型字段的 Elasticsearch 索引?

标签 elasticsearch

{
 "settings": {
  "index": {
   "mapping": {
    "ignore_malformed": "true",
    "include_type_name": "true"
   }
  }
 },
  "mappings": {
   "properties": {
    "address": {
     "type": "text",
     "field": {
      "type": {
       "type": "keyword"
      },
      "ip": {
       "type": "ip"
      },
      "comment": {
       "analyzer": "whitespace",
       "type": "text"
      }
     }
    }
   }
 }

错误: elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', "无法解析 ID 为 'UA7RSHUBK7u8_ZjU0JQR' 的文档中 [text] 类型的字段 [address]** **

这是我的代码和错误信息。 如何修复此映射 json? 你认为是什么原因造成的? 感谢您的回复。

最佳答案

The include_type_name parameter in the index creation, index template, and mapping APIs will default to false. Setting the parameter at all will result in a deprecation warning from Elasticsearch 7.x

fields设置允许对同一索引中的同名字段有不同的设置。

修改后的索引映射为:

{
  "settings": {
    "index": {
      "mapping": {                   <-- note this
        "ignore_malformed": "true"
      }
    }
  },
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "fields": {          <-- note this
          "type": {
            "type": "keyword"
          },
          "ip": {
            "type": "ip"
          },
          "comment": {
            "analyzer": "whitespace",
            "type": "text"
          }
        }
      }
    }
  }
}

请参阅 Elasticsearch 官方文档 fields了解更多。

添加一个工作示例,其中包含索引数据、索引映射(与上面给出的相同)、搜索查询和搜索结果。

索引数据:

{
  "address":"Khajrana circle"
}
{
  "address":"indore"
}
{
  "address":"192.168.1.1"
}

搜索查询:

{
  "query": {
    "multi_match": {
      "query": "indore",
      "fields": [
        "address",
        "address.type",
        "address.comment"
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "64455730",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0925692,
        "_ignored": [
          "address.ip"
        ],
        "_source": {
          "address": "indore"
        }
      }
    ]

关于elasticsearch - 如何创建具有多类型字段的 Elasticsearch 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64455730/

相关文章:

elasticsearch - Elasticsearch 中的嵌套文档

ruby-on-rails - 重新轮胎 Elasticsearch 多表/索引搜索

Elasticsearch 日期聚合

linux - Elasticsearch IP与实际IP不同

ruby-on-rails - ElasticSearch在生产环境中的行为有所不同

elasticsearch - 更改任何NON-INDEXED字段的映射时,是否需要强制为所有较旧的文档重新编制索引?

php - Elasticsearch仅按字母排序而不按数字排序

search - Elasticsearch每个groupId的聚合唯一属性

xml - 使用 Logstash 解析来自 Filebeat 的 XML 数据

django - 同步 mysql 和 elasticsearch 的最佳实践是什么?