2

我对 ArangoDB 比较陌生,在阅读完文档后,我试图为一个新项目实施它。

我有一个文档集合,每个文档中有一个列表,其中包含许多术语。我正在使用 java 驱动程序,并且想查询其列表与我拥有的列表中的任何元素匹配的文档。

例子:

Document 1
{
    tokens["blue", "red", "green"]
}

Document 2
{
    tokens["black", "red", "yellow"]
}

myArrayList:
["purple", "red"]

由于我尝试查询的 ArrayList 包含单词“red”,因此我应该同时看到文档 1 和文档 2。理想情况下,我只会看到文档 ID 和匹配的颜色。

根据我对 AQL 的了解,在半伪代码中:

FOR document IN documents FILTER document.tokens CONTAINS myArrayList RETURN document.token.color && document._id

我通常会返回整个文档对象,然后只访问我需要的任何内容。如果它更容易,我可以这样做。例如:

FOR document IN documents FILTER document.tokens CONTAINS myArrayList RETURN document
4

3 回答 3

4

I suggest using the IN operator for filtering as follows:

FOR document IN documents
  FILTER document.tokens IN @myArrayList
  RETURN document

This will only return a document if the tokens attribute is an array and contains any of the values contained in the @myArrayList bind parameter.

于 2015-01-09T08:42:31.667 回答
1

I have found an answer to my question on the ArangoDB Google Group. I am linking to it as it was very difficult for me to locate a solution: https://groups.google.com/forum/#!newtopic/arangodb/arangodb/fen4Nr7N4Uo

I have adapted the code there to work for my case: (Credit to stj's comment for fixing what I wrote)

FOR document IN documents LET contains = 
(FOR color IN document.tokens FILTER MATCHES(color, @myArrayList) RETURN color)
FILTER LENGTH(contains) > 0 RETURN document
于 2015-01-09T00:43:12.793 回答
0

If you want to find documents, which at least contain all specified colors, in any order, you can use a query like this:

LET lenArrayList = LENGTH(@myArrayList)

FOR doc IN documents
    FILTER HAS(doc, "tokens") // avoid bad calls to INTERSECTION()
    FILTER LENGTH(INTERSECTION(@myArrayList, doc.tokens)) == lenArrayList
    RETURN doc

myArrayList: ["yellow","red"]

Result: document 2, because it contains red and yellow.

于 2015-01-16T17:59:41.353 回答