I want to use an index and saw this:
class Products_ByCategoryName_JS extends AbstractJavaScriptIndexCreationTask {
constructor () {
super();
const { load } = this.mapUtils();
this.map("Products", product => {
return {
// Call method 'load' to load the related Category document
// The document ID to load is specified by 'product.Category'
// The Name field from the related Category document will be indexed
categoryName: load(product.Category, "Categories").Name
// Since NoTracking was Not specified,
// then any change to either Products or Categories will trigger reindexing
};
});
}
}
and this is how I execute the query
const matchingProducts = await session
.query({indexName: "Products/ByCategoryName"})
.whereEquals("CategoryName", "Beverages")
.all();
its from the docs, ok but how can I execute the index Class above Products_ByCategoryName_JS,
so where it is executed or where can I included it in my code ?
€: got this error:
InvalidArgumentException: Map is required to generate an index, you cannot create an index without a valid Map property (in index Users/ByName)
index:
// @ts-nocheck
import { AbstractJavaScriptIndexCreationTask } from "ravendb";
export class Users_ByName extends AbstractJavaScriptIndexCreationTask<{name: string}[]> {
constructor () {
super();
this.map = `
from users in docs.UsersData
select new {
Name = users.name
}`;
}
}
query:
app.get('/', async (req, res) => {
try {
await new Users_ByName().execute(store);
let session = store.openSession();
const queryOnIndex = session.query({indexName: 'UsersData/ByName'}).whereEquals('name', 'Johnse');
const d = await queryOnIndex.all();
res.json('s');
} catch(e) {
console.log(e);
res.send(e);
}
});
what I am doing wrong ? My collectionName is UsersData and my field name is "name" that I want to index
To execute/deploy the index to the server, call:
await new Products_ByCategoryName_JS().execute(documentStore);
See demo "static indexes overview" in the Node.js client:
https://demo.ravendb.net/demos/nodejs/static-indexes/static-indexes-overview
https://demo.ravendb.net/demos/nodejs/static-indexes/static-indexes-overview#step-3
In the latest example you provided - the index class name is: Users_ByName
, but in the query, you use index name: UsersData/ByName
.
In the query you should use the index name from the 'class' and only replace _
with /
.
So, in your query you should use: session.query({indexName: 'Users/ByName'})
More info about creating and deploying indexes is available in:
https://ravendb.net/docs/article-page/latest/nodejs/indexes/creating-and-deploying
https://ravendb.net/docs/article-page/latest/nodejs/indexes/creating-and-deploying#naming-convention