【问题标题】:Find all children of user with Searchkick使用 Searchkick 查找用户的所有孩子
【发布时间】:2015-12-20 21:31:03
【问题描述】:

我正在尝试查找给定用户的儿童帐户的所有记录。

我现在拥有的:

@parent = @user.id
@children = User.search ("*"), 
  page: params[:page], per_page: 20, 
    # Discard account that don't have coordinates to avoid distance errors
    where: {
      coordinates: {not: nil},
      role: :organization,
      parent_id: @parent
    },
    order: {_geo_distance: {coordinates: {lat: @coordinates[1], lon: @coordinates[0]}, order: "asc", unit: "mi"} }

这会运行以下查询:

curl http://localhost:1234/users_development/_search?pretty -d '{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "and": [
          {
            "not": {
              "filter": {
                "missing": {
                  "field": "coordinates",
                  "existence": true,
                  "null_value": true
                }
              }
            }
          },
          {
            "term": {
              "role": "organization"
            }
          },
          {
            "term": {
              "parent_id": {
                "$oid": "44fa9f8962656e0404bd3310"
              }
            }
          }
        ]
      }
    }
  },
  "size": 20,
  "from": 0,
  "sort": {
    "_geo_distance": {
      "coordinates": {
        "lat": -118.3919,
        "lon": 34.1379
      },
      "order": "asc",
      "unit": "mi"
    }
  },
  "fields": []
}'

当我在控制台中查找其中一个子用户时,我看到了 parent_id:<BSON::ObjectId:0x70205146889309 data=44fa9f8962656e0404bd3310>

我运行的查询给出了Searchkick::InvalidQueryError ([400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures...

我很确定我输入错误的查询不知何故。正确的语法是什么?

这是完整的错误:

Searchkick::InvalidQueryError ([400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[MRKTgyLfTGSnADDAIoMdLg][users_development_20151220094401447][0]: SearchParseException[[users_development_20151220094401447][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"and\":[{\"not\":{\"filter\":{\"missing\":{\"field\":\"coordinates\",\"existence\":true,\"null_value\":true}}}},{\"term\":{\"role\":\"organization\"}},{\"term\":{\"parent_id\":{\"$oid\":\"44fa9f8962656e0404bd3310\"}}}]}}},\"size\":20,\"from\":0,\"sort\":{\"_geo_distance\":{\"coordinates\":{\"lat\":-118.3919,\"lon\":34.1379},\"order\":\"asc\",\"unit\":\"mi\"}},\"fields\":[]}]]]; nested: QueryParsingException[[users_development_20151220094401447] [term] filter does not support [$oid]]; }{[MRKTgyLfTGSnADDAIoMdLg][users_development_20151220094401447][1]: SearchParseException[[users_development_20151220094401447][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"and\":[{\"not\":{\"filter\":{\"missing\":{\"field\":\"coordinates\",\"existence\":true,\"null_value\":true}}}},{\"term\":{\"role\":\"organization\"}},{\"term\":{\"parent_id\":{\"$oid\":\"44fa9f8962656e0404bd3310\"}}}]}}},\"size\":20,\"from\":0,\"sort\":{\"_geo_distance\":{\"coordinates\":{\"lat\":-118.3919,\"lon\":34.1379},\"order\":\"asc\",\"unit\":\"mi\"}},\"fields\":[]}]]]; nested: QueryParsingException[[users_development_20151220094401447] [term] filter does not support [$oid]]; }{[MRKTgyLfTGSnADDAIoMdLg][users_development_20151220094401447][2]: SearchParseException[[users_development_20151220094401447][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"and\":[{\"not\":{\"filter\":{\"missing\":{\"field\":\"coordinates\",\"existence\":true,\"null_value\":true}}}},{\"term\":{\"role\":\"organization\"}},{\"term\":{\"parent_id\":{\"$oid\":\"44fa9f8962656e0404bd3310\"}}}]}}},\"size\":20,\"from\":0,\"sort\":{\"_geo_distance\":{\"coordinates\":{\"lat\":-118.3919,\"lon\":34.1379},\"order\":\"asc\",\"unit\":\"mi\"}},\"fields\":[]}]]]; nested: QueryParsingException[[users_development_20151220094401447] [term] filter does not support [$oid]]; }{[MRKTgyLfTGSnADDAIoMdLg][users_development_20151220094401447][3]: SearchParseException[[users_development_20151220094401447][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"and\":[{\"not\":{\"filter\":{\"missing\":{\"field\":\"coordinates\",\"existence\":true,\"null_value\":true}}}},{\"term\":{\"role\":\"organization\"}},{\"term\":{\"parent_id\":{\"$oid\":\"44fa9f8962656e0404bd3310\"}}}]}}},\"size\":20,\"from\":0,\"sort\":{\"_geo_distance\":{\"coordinates\":{\"lat\":-118.3919,\"lon\":34.1379},\"order\":\"asc\",\"unit\":\"mi\"}},\"fields\":[]}]]]; nested: QueryParsingException[[users_development_20151220094401447] [term] filter does not support [$oid]]; }{[MRKTgyLfTGSnADDAIoMdLg][users_development_20151220094401447][4]: SearchParseException[[users_development_20151220094401447][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"and\":[{\"not\":{\"filter\":{\"missing\":{\"field\":\"coordinates\",\"existence\":true,\"null_value\":true}}}},{\"term\":{\"role\":\"organization\"}},{\"term\":{\"parent_id\":{\"$oid\":\"44fa9f8962656e0404bd3310\"}}}]}}},\"size\":20,\"from\":0,\"sort\":{\"_geo_distance\":{\"coordinates\":{\"lat\":-118.3919,\"lon\":34.1379},\"order\":\"asc\",\"unit\":\"mi\"}},\"fields\":[]}]]]; nested: QueryParsingException[[users_development_20151220094401447] [term] filter does not support [$oid]]; }]","status":400}):

【问题讨论】:

  • 您能否添加您看到的完整错误,尤其是 shardFailures 之后的错误?
  • 当然。添加到我上面的帖子中。

标签: elasticsearch searchkick


【解决方案1】:

根据错误

[term] filter does not support [$oid]]

问题出在以下部分

      {
        "term": {
          "parent_id": {
            "$oid": "44fa9f8962656e0404bd3310"
          }
        }
      }

你只需要这样写:

      {
        "term": {
          "parent_id": "44fa9f8962656e0404bd3310"
        }
      }

【讨论】:

  • 我的问题是你是如何用 searchkick 写的。显然,我知道我需要将 parent_id 字段与父母的 id 匹配。将“parent_id:@parent”添加到“where”子句不起作用。添加“term:{parent_id:@parent}”也不会。在“where”子句之后添加“term: {parent_id: @parent}”也不起作用。
  • 哦,好的,我明白了。那么this answer 很可能就是您要找的。​​span>
  • 谢谢,但这没有帮助。还有这个:stackoverflow.com/questions/23544048/… 没有帮助。我将这个问题作为问题发布在 github 上,希望能收到回复。感谢您提供帮助。
  • 对于那些愿意关注的人,@Ben 发布的问题在这里:github.com/ankane/searchkick/issues/589
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多