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

huangapple go评论75阅读模式
英文:

How can I fix this Arango (Golang) AQL Query to use an Index?

问题

嗨,我正在尝试使用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)

任何帮助或指导将非常有帮助!

英文:

Hey I'm having this issue trying to use an index in AQL. I tried checking out the documentation and other SO question but couldn't find any examples.

This is my code in 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,
}

But I keep getting this error that I can't figure out:
AQL: syntax error, unexpected identifier, expecting end of query string near 'USE INDEX @carIndex\n\t\t...' at position (while parsing)

Any help or direction would be incredibly helpful!

答案1

得分: 1

解决方案是使用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,
}
英文:

Solution was to use 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,
}

However I was informed by the Arango team that ANY IN cannot utilize a regular index. This is only supported by ArangoSearch. So the proper way of writing this is:

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,
}

huangapple
  • 本文由 发表于 2023年3月16日 23:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75758402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定