Graphene Django 查询 Elasticsearch
Posted
技术标签:
【中文标题】Graphene Django 查询 Elasticsearch【英文标题】:Graphene Django to Query Elasticsearch 【发布时间】:2019-10-31 20:03:24 【问题描述】:我的数据存储在 elasticsearch 中,而且非常大。所以我想使用 graphql 来正确查询它。我正在使用 django 石墨烯包来查询它。对于数据库模型,它工作正常。我的 elasticsearch json 架构
https://pastebin.com/EQBnnCBU 下面是我的类型定义和查询代码
https://pastebin.com/fsr9V1Rf
问题是我无法理解如何为弹性 json 架构编写查询架构。
只需要初步帮助或任何可以帮助我的解释
我已经在这里检查了答案django-graphene without model 但没有帮助
我当前的 ElasticType 架构
class ElasticType(graphene.ObjectType):
id = graphene.ID()
index = graphene.String()
found = graphene.String()
properties = graphene.String()
【问题讨论】:
【参考方案1】:你可以试试graphene-elastic。
如示例所述,您需要声明要过滤哪些字段,它会自动使它们可用于使用指定的查找过滤器进行过滤。
一些代码部分(与您的项目无关,只是为了说明它是如何工作的,从项目文档中复制而来)。
示例文档定义
import datetime
from elasticsearch_dsl import (
Boolean,
Date,
Document,
Keyword,
Nested,
Text,
Integer,
)
class Post(Document):
title = Text(
fields='raw': Keyword()
)
content = Text()
created_at = Date()
published = Boolean()
category = Text(
fields='raw': Keyword()
)
tags = Text(
analyzer='snowball',
fields='raw': Keyword(multi=True),
multi=True
)
num_views = Integer()
class Index:
name = 'blog_post'
settings =
'number_of_shards': 1,
'number_of_replicas': 1,
'blocks': 'read_only_allow_delete': None,
def add_tag(self, name):
self.tags.append(name)
def save(self, ** kwargs):
self.created_at = datetime.datetime.now()
return super().save(** kwargs)
示例架构定义
import graphene
from graphene_elastic import (
ElasticsearchObjectType,
ElasticsearchConnectionField,
)
from graphene_elastic.filter_backends import (
FilteringFilterBackend,
SearchFilterBackend,
)
from graphene_elastic.constants import (
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_EXCLUDE,
LOOKUP_QUERY_IN,
)
# Object type definition
class Post(ElasticsearchObjectType):
class Meta(object):
document = PostDocument
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
SearchFilterBackend,
]
filter_fields =
'title':
'field': 'title.raw',
'lookups': [
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
'default_lookup': LOOKUP_FILTER_TERM,
,
'category': 'category.raw',
'tags': 'tags.raw',
'num_views': 'num_views',
search_fields =
'title': 'boost': 4,
'content': 'boost': 2,
'category': None,
# Query definition
class Query(graphene.ObjectType):
all_post_documents = ElasticsearchConnectionField(Post)
# Schema definition
schema = graphene.Schema(query=Query)
示例查询
query PostsQuery
allPostDocuments(filter:
category:terms:["Elastic", "Python"]
)
edges
node
id
title
category
content
createdAt
comments
或者:
allPostDocuments(filter:
category:term:"Python",
numViews:gt:"700"
)
edges
node
category
title
comments
numViews
研究文档以获取更多信息。
【讨论】:
我还需要在嵌套数据中进行搜索。就像我有一个关键用户,然后在其地址中。那么我可以获取地址吗? @pawan:那个功能是not yet available。以上是关于Graphene Django 查询 Elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章
Graphene-Django:在模式中结合查询对象(只接受第一个参数)
Graphene Django 查询 Elasticsearch
graphene-django:查询所有模型字段而不是请求的字段