【问题标题】:Python + ElasticSearch: Mapper Parsing Exceptions for join fieldPython + ElasticSearch:join字段的Mapper解析异常
【发布时间】:2023-01-25 04:33:32
【问题描述】:

我正在使用 ElasticSearch 8.3.2 来存储我拥有的一些数据。数据由代谢物和每种代谢物的几个“研究”组成,每个研究又包含浓度值。我还使用 Python ElasticSearch client 与后端通信,效果很好。 为了将代谢物与研究相关联,我正在考虑使用 here 所述的连接字段。

我已经定义了这个索引映射:

INDEXMAPPING_MET = {
    "mappings": {
        "properties": {
            "id": {"type": "keyword"},
            "entry_type": {"type": "text"},
            "pc_relation": {
                "type": "join",
                "relations": {
                    "metabolite": "study"
                }
            },
            "concentration": {
                "type": "nested",
            }
        }
    }
}

pc_relation 是这里的连接字段,metabolites 是每个研究文档的父文档。 例如,我可以使用 Python 客户端创建代谢物条目(父文档)

self.client.index(index="metabolitesv2", id=metabolite, body=json.dumps({
                #[... some other fields here]
                "pc_relation": {
                    "name": "metabolite",
                },
            }))

但是,一旦我尝试添加子文档,我就会得到 mapping_parser_exception。值得注意的是,我只在尝试添加 pc_relation 字段时遇到此异常,任何其他字段都可以正常工作,如果我省略连接字段,我可以创建文档。这是我正在尝试创建的研究文档的示例(在同一索引上):

self.client.index(index="metabolitesv2", id=study, body=json.dumps({
                #[... some other fields here]
                "pc_relation": {
                    "name": "study",
                    "parent": metabolite_id
                },
            }))

起初我以为可能存在一些打字问题,但不幸的是,将所有内容都转换为字符串并没有改变结果。对于错误可能出在哪里的任何帮助,我真的很感激,因为我不确定问题出在哪里——从官方 ES 文档和其他 Python+ES 项目中我可以看出,我并没有做任何不同的事情。

尝试过:创建一个带有连接字段的索引,创建一个父文档,创建一个与父文档有连接关系的子文档。期待: 文档被创建并可以使用has_childhas_parent 标签进行查询。结果: 尝试创建子文档时映射解析器异常

【问题讨论】:

    标签: python elasticsearch exception indexing mapping


    【解决方案1】:

    Tldr;

    您需要在为子文档编制索引时provide a routing value

    路由值是强制性的,因为父文档和子文档必须在同一个分片上建立索引

    默认情况下,一个文档的路由值是它的_id,所以实际上你需要在索引子文档时提供父文档的_id

    解决方案

    self.client.index(index="metabolitesv2", id=study, routing=metabolite, body=json.dumps({
        #[... some other fields here]
        "pc_relation": {
            "name": "study",
            "parent": metabolite_id
        },
    }))
    

    重现

    PUT 75224800
    {
      "settings": {
        "number_of_shards": 4
      }, 
      "mappings": {
        "properties": {
          "id": {
            "type": "keyword"
          },
          "pc_relation": {
            "type": "join",
            "relations": {
              "metabolite": "study"
            }
          }
        }
      }
    }
    
    PUT 75224800/_doc/1
    {
      "id": "1",
      "pc_relation": "metabolite"
    }
    
    # No routing Id this is going to fail
    PUT 75224800/_doc/2
    {
      "id": 2,
      "pc_relation":{
        "name": "study",
        "parent": "1"
      }
    }
    
    PUT 75224800/_doc/3
    {
      "id": "3",
      "pc_relation": "metabolite"
    }
    
    PUT 75224800/_doc/4?routing=3
    {
      "id": 2,
      "pc_relation":{
        "name": "study",
        "parent": "3"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2013-04-13
      相关资源
      最近更新 更多