ArangoSearch是ArangoDB的一个功能强大的全文搜索引擎。要在JSON数组对象上使用ArangoSearch,您需要遵循以下步骤:
const { Database } = require("@arangodb");
const db = new Database();
// 创建一个名为"myCollection"的集合
const myCollection = db._create("myCollection");
// 启用ArangoSearch
db._query(`
FOR c IN myCollection
COLLECT WITH COUNT INTO length
RETURN length
`).toArray();
const arangoSearchView = db._createView("myArangoSearchView", "arangosearch", {
collection: "myCollection",
fields: {
// 定义要索引的字段
name: {},
age: {}
}
});
const documents = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
{ name: "Charlie", age: 35 }
];
documents.forEach(doc => myCollection.save(doc));
// 查找年龄大于25的所有文档
const result = db._query(`
FOR doc IN myArangoSearchView
FILTER doc.age > 25
RETURN doc
`).toArray();
console.log(result);
这将返回年龄大于25的所有文档:
[
{ "_key": "3", "_id": "myCollection/3", "_rev": "_a1b2c3--_", "name": "Charlie", "age": 35 }
]
领取专属 10元无门槛券
手把手带您无忧上云