ArangoSearch Examples

Assume the following collections were initially defined in a database usingthe following arangosh commands:

  1. c0 = db._create("ExampleCollection0");
  2. c1 = db._create("ExampleCollection1");
  3. c0.save({ i: 0, name: "full", text: "是一个 多模 型数 据库" });
  4. c0.save({ i: 1, name: "half", text: "是一个 多模" });
  5. c0.save({ i: 2, name: "other half", text: "型数 据库" });
  6. c0.save({ i: 3, name: "quarter", text: "是一" });
  7. c1.save({ a: "foo", b: "bar", i: 4 });
  8. c1.save({ a: "foo", b: "baz", i: 5 });
  9. c1.save({ a: "bar", b: "foo", i: 6 });
  10. c1.save({ a: "baz", b: "foo", i: 7 });

A View with default parameters can be created by calling db._createView()(see JavaScript Interface for Views):

  1. v0 = db._createView("ExampleView", "arangosearch", {});

Then link the collections to the View, define which properties shall be indexedand with which Analyzers the values are supposed to be processed:

  1. v0 = db._view("ExampleView");
  2. v0.properties({
  3. links: {
  4. /* collection Link 0 with additional custom configuration: */
  5. 'ExampleCollection0':
  6. {
  7. /* examine fields of all linked collections,
  8. using default configuration: */
  9. includeAllFields: true,
  10. fields:
  11. {
  12. /* a field to apply custom configuration
  13. that will index English text: */
  14. name:
  15. {
  16. analyzers: ["text_en"]
  17. },
  18. /* another field to apply custom configuration
  19. that will index Chinese text: */
  20. text:
  21. {
  22. analyzers: ["text_zh"]
  23. }
  24. }
  25. },
  26. /* collection Link 1 with custom configuration: */
  27. 'ExampleCollection1':
  28. {
  29. /* examine all fields using default configuration: */
  30. includeAllFields: true,
  31. fields:
  32. {
  33. a:
  34. {
  35. /* a field to apply custom configuration
  36. that will index English text: */
  37. analyzers: ["text_en"]
  38. }
  39. }
  40. }
  41. }
  42. }
  43. );

You have to wait a few seconds for the View to update its index.Then you can query it:

  1. db._query(`FOR doc IN ExampleView
  2. SEARCH PHRASE(doc.text, "型数 据库", "text_zh") OR
  3. ANALYZER(STARTS_WITH(doc.b, "ba"), "text_en")
  4. SORT TFIDF(doc) DESC
  5. RETURN doc`);

The result will include all documents from both linked collections that havethe Chinese phrase 多模 型数 at any position in the text attribute valueor the property b starting with ba in English. Additionally,descendant sorting using the TF-IDF scoring algorithm will be applied.

  1. [
  2. {
  3. "_key" : "120",
  4. "_id" : "ExampleCollection0/120",
  5. "_rev" : "_XPoMzCi--_",
  6. "i" : 0,
  7. "name" : "full",
  8. "text" : "是一个 多模 型数 据库"
  9. },
  10. {
  11. "_key" : "124",
  12. "_id" : "ExampleCollection0/124",
  13. "_rev" : "_XPoMzCq--_",
  14. "i" : 2,
  15. "name" : "other half",
  16. "text" : "型数 据库"
  17. },
  18. {
  19. "_key" : "128",
  20. "_id" : "ExampleCollection1/128",
  21. "_rev" : "_XPoMzCu--_",
  22. "a" : "foo",
  23. "b" : "bar",
  24. "c" : 0
  25. },
  26. {
  27. "_key" : "130",
  28. "_id" : "ExampleCollection1/130",
  29. "_rev" : "_XPoMzCy--_",
  30. "a" : "foo",
  31. "b" : "baz",
  32. "c" : 1
  33. }
  34. ]

Use cases

The data contained in our View looks like that:

  1. { "id": 1, "body": "ThisIsAVeryLongWord" }
  2. { "id": 2, "body": "ThisIsNotSoLong" }
  3. { "id": 3, "body": "ThisIsShorter" }
  4. { "id": 4, "body": "ThisIs" }
  5. { "id": 5, "body": "ButNotThis" }

We now want to search for documents where the attribute body starts with"ThisIs". A simple AQL query executing this prefix search:

  1. FOR doc IN viewName
  2. SEARCH STARTS_WITH(doc.body, 'ThisIs')
  3. RETURN doc

It will find the documents with the ids 1, 2, 3, 4, but not 5.