我如何修复此Arango(Golang)AQL查询以使用索引?

vfhzx4xs  于 2023-03-21  发布在  Go
关注(0)|答案(1)|浏览(103)

嘿,我有这个问题,试图在AQL中使用索引。我试着检查文档和其他SO问题,但找不到任何例子。
这是我在Golang的代码:

query := `
  LET results = FLATTEN(
    FOR doc IN @@collectionName01
    FILTER @brand == doc.name
    RETURN doc.cars)
  FOR doc IN @@collectionName02
    FILTER results ANY IN doc.cars
    RETURN doc
  USE INDEX @carIndex`

bindVars := map[string]interface{}{
  "@collectionName01": Brand_Collection,
  "brand":              brand,
  "@collectionName02": Car_Collection,
  "carIndex": Car_Index,
}

但我一直收到这个错误,我不能弄清楚:AQL: syntax error, unexpected identifier, expecting end of query string near 'USE INDEX @carIndex\n\t\t...' at position (while parsing)
任何帮助或方向都将是难以置信的帮助!

guicsvcw

guicsvcw1#

解决方案是使用OPTIONS {indexHint: @carIndex}

query := `
  LET results = FLATTEN(
    FOR doc IN @@collectionName01
    FILTER @brand == doc.name
    RETURN doc.cars)
  FOR doc IN @@collectionName02 OPTIONS {indexHint: @carIndex}
    FILTER results ANY IN doc.cars
    RETURN doc
bindVars := map[string]interface{}{
  "@collectionName01": Brand_Collection,
  "brand":              brand,
  "@collectionName02": Car_Collection,
  "carIndex": Car_Index,
}

但是Arango团队告诉我ANY IN不能使用常规索引,只有ArangoSearch支持,所以正确的写法是:

query := `
  FOR doc1 IN @@collectionName01 FILTER @brand == doc1.name
    FOR doc2 IN @@collectionName02
    FILTER doc1.cars ANY IN doc2.cars
    RETURN doc2`
bindVars := map[string]interface{}{
  "@collectionName01": Brand_Collection,
  "brand":              brand,
  "@collectionName02": Car_Collection,
}

相关问题