如何使用多类型字段创建elasticsearch索引?

bqf10yzr  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(433)
{
 "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', "failed to parse field [address] of type [text] in document with id 'UA7RSHUBK7u8_ZjU0JQR'****
这是我的代码和错误信息。如何修复此Mapjson?你认为是什么引起的?谢谢你的回复。

nlejzf6q

nlejzf6q1#

mapper_parsing_exception 当elasticsearch由于各种原因无法根据文档的Map正确解析文档时,会出现异常,例如:如果在Map中定义了类型为的字段 object 试着储存 text field,一个类似的问题被问到,我在哪里更详细地解释了它。
如错误消息中所述,您的文档id UA7RSHUBK7u8_ZjU0JQR 有一个不适合你的格式 address 字段中,您需要为问题中的该id提供文档json,并完成错误消息以进一步调试问题。

vjhs03f7

vjhs03f72#

索引创建、索引模板和MapAPI中的include\ type\ name参数将默认为false。设置此参数将导致elasticsearch 7.x发出弃用警告
这个 fields 允许对同一索引中同名字段进行不同的设置。
修改后的索引Map将是:

{
  "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官方文档以了解更多信息。
添加一个使用索引数据、索引Map(与上面给出的相同)、搜索查询和搜索结果的工作示例。
索引数据:

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

相关问题