简体   繁体   中英

ArangoDB Query for a certain number of matches to an Array

In ArangoDB I have a document with a field for storing "tokens". A token is an array of int values. Eg:

token:
[0] = 5000
[1] = 250
[2] = 300
etc... 

A token may have up to about 50 of these values. I want to query for documents who have at least 4 tokens in common with my ArrayList. For example, if I have an ArrayList with the values:

[0] = 1;
[1] = 200;
[2] = 400;
[4] = 600;
[5] = 570;

I would get a list of documents that have at least four token values in common with my array. Currently I am just doing:

String query = "FOR doc IN docs LET contained = (FOR token IN @tokenValues FILTER token IN doc.tokens[*]) FILTER LENGTH(contained) > 0 RETURN doc";
            bindVars = new MapBuilder().put("tokenValues", tokenArray).get();

and then I get documents that have at least 1 token in common, and search through all of these for the ones that have at least 4 in common. I would imagine that there is a way to only get the results of the ones with at least 4 in common, but I can't seem to figure out how one would go about doing this.

I think the following AQL query will do it:

FOR doc IN docs 
  LET contained = (
    FOR token IN doc.tokens 
      FILTER token IN @tokens 
      RETURN token
    ) 
  FILTER LENGTH(contained) >= 4 
  RETURN doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM