需要了解的情况下加入ElasticSearch 7+

问题描述

我无法使用elasticsearch_dsl加入联接。从根本上来说,我认为我不知道如何在ElasticsSearch 7+中构建连接

这是我正在做的一个精简示例:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import (Document,Integer,Join,Text)

class BaseDocument(Document):
    class Index:
        name = "my_index"
        using = Elasticsearch("http...")


class Purchase(BaseDocument):
    amount = Integer()


class User(BaseDocument):
    name = Text()
    user_purchases = Join(relations={"user": "purchase"})

    def add_purchase(self,*,amount: int):
        purchase = Purchase(
            _routing=self.Meta.id,user={"name": "purchase","parent": self.Meta.id},amount=amount,)
        purchase.save()
        return purchase


User.init()
user = User(_id=1,name="yas")
user.save()
user.add_purchase(amount=100)

哪个映射为:

{
  "my_index": {
    "mappings": {
      "properties": {
        "amount": {
          "type": "long"
        },"user": {
          "properties": {
            "name": {
              "type": "text","fields": {
                "keyword": {
                  "type": "keyword","ignore_above": 256
                }
              }
            },"parent": {
              "type": "long"
            }
          }
        },"user_purchases": {
          "type": "join","eager_global_ordinals": true,"relations": {
            "user": "purchase"
          }
        }
      }
    }
  }
}

文件

{
  "took": 578,"timed_out": false,"_shards": {
    "total": 1,"successful": 1,"skipped": 0,"Failed": 0
  },"hits": {
    "total": {
      "value": 2,"relation": "eq"
    },"max_score": 1.0,"hits": [
      {
        "_index": "my_index","_type": "_doc","_id": "1","_score": 1.0,"_source": {
          "name": "yas"
        }
      },{
        "_index": "my_index","_id": "bSTqDHQBsgOLaltlJlWo","_routing": "1","_source": {
          "user": {
            "name": "purchase","parent": "1"
          },"amount": 100
        }
      }
    ]
  }
}

但是子搜索失败:

{
    "query": {
        "has_child": {
            "type": "purchase","query": {
                "match_all": {}
            }
        }
    }
}

我预计会有1次命中,但是得到零:

  "hits": {
    "total": {
      "value": 0,"max_score": null,"hits": []
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...