或者可以将django的干草堆中的距离过滤器与距离过滤器结合使用吗?

发布于 2025-01-24 21:44:25 字数 755 浏览 0 评论 0原文

我一直在使用“或子”内的常规django查询过滤器使用距离过滤器:

# either it's within the preset distance of the location
# OR it's not an in-person event
self.filter(Q(location__distance_lt=(geometry, LOCATION_WITHIN_D)) | ~Q(type=Event.IN_PERSON))

我想使用SQ进行类似的过滤,作为Haystack查询的一部分,使用SQ:

queryset = queryset.filter(SQ(location__distance_lt=(geometry, LOCATION_WITHIN_D)) | ~SQ(type=Event.IN_PERSON))

但是我会收回错误:并非所有参数在字符串格式期间转换 - ,这仅发生在dange_lt查询部分中,而不是〜sq(type ..)

我能够通过我的haystack搜索查询应用距离过滤使用

queryset = queryset.dwithin('location', geometry, LOCATION_WITHIN_D)`

,但我希望能够在此款中具有“或”条件。

原始查询甚至可以吗?我如何为Elasticsearch构建这样的原始查询,并仍将其作为Haystack查询的一部分执行?

I've been using a distance filter using regular django query filters within an OR-clause:

# either it's within the preset distance of the location
# OR it's not an in-person event
self.filter(Q(location__distance_lt=(geometry, LOCATION_WITHIN_D)) | ~Q(type=Event.IN_PERSON))

I want to do a similar filtering as part of a haystack query, using SQ:

queryset = queryset.filter(SQ(location__distance_lt=(geometry, LOCATION_WITHIN_D)) | ~SQ(type=Event.IN_PERSON))

but instead I get back the error: not all arguments converted during string formatting - and that only happens with the distance_lt query part, not the ~SQ(type..)

I'm able to apply the distance filtering with my haystack search query by using

queryset = queryset.dwithin('location', geometry, LOCATION_WITHIN_D)`

but I want to be able to have that 'or' condition in this sub-clause.

Is this even possible with raw querying? How can I construct such a raw query for ElasticSearch and still execute it as part of my haystack query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2025-01-31 21:44:26

您可以针对Elasticsearch连接执行搜索。

from django.contrib.gis.measure import D
from haystack.query import SearchQuerySet

sqs = SearchQuerySet('default')
be = sqs.query.backend
LOCATION_WITHIN_D = D(km=0.1)
geometry = {
    "lat": 60.1939,
    "lon": 11.1003
}

raw_query = {
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "type": Event.IN_PERSON
                }
              },
              {
                "geo_distance": {
                  "distance": str(LOCATION_WITHIN_D),
                  "location": geometry
                }
              }
            ]
          }
        }
      ]
    }
  }
}
result = be.conn.search(body=raw_query, index='name-of-your-index')

You can perform the search against the Elasticsearch connection.

from django.contrib.gis.measure import D
from haystack.query import SearchQuerySet

sqs = SearchQuerySet('default')
be = sqs.query.backend
LOCATION_WITHIN_D = D(km=0.1)
geometry = {
    "lat": 60.1939,
    "lon": 11.1003
}

raw_query = {
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "type": Event.IN_PERSON
                }
              },
              {
                "geo_distance": {
                  "distance": str(LOCATION_WITHIN_D),
                  "location": geometry
                }
              }
            ]
          }
        }
      ]
    }
  }
}
result = be.conn.search(body=raw_query, index='name-of-your-index')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文