加入elasticsearch 7+,需要了解

ffscu2ro  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(0)|浏览(252)

我无法使用elasticsearch\u dsl使连接工作。从根本上说,我不知道如何构造ElasticsSearch7中的连接+
下面是我正在做的一个精简示例:

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)

从而得出以下Map:

{
  "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",
        "_type": "_doc",
        "_id": "bSTqDHQBsgOLaltlJlWo",
        "_score": 1.0,
        "_routing": "1",
        "_source": {
          "user": {
            "name": "purchase",
            "parent": "1"
          },
          "amount": 100
        }
      }
    ]
  }
}

但子搜索失败:

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

我期待1次命中,但得到零:

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题