Getting started with ArangoSearch Views

The DDL configuration

DDL is a datadefinition language or data description language for defining data structures,especially database schemas.

All DDL operations on Views can be done via JavaScript or REST calls. The DDLsyntax follows the well established ArangoDB guidelines and thus is verysimilar between the JavaScript interface for viewsand the HTTP interface for views.This articleuses the JavaScript syntax.

Assume the following collections were initially defined in a database usingthe following 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 });

Creating a View (with default parameters)

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

Linking created View with a collection and adding indexing parameters

  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. );

Query data using created View with linked collections

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

Examine query result

Result of the latter query will include all documents from both linkedcollections that include 多模 型数 phrase in Chinese at any part of textproperty or b property in English that starts with ba. Additionally,descendant sorting using TFIDF algorithmwill be applied during a search:

  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. ]