Search code examples
node.jselasticsearchclientelastic-enterprise-search

Query meta engine by list fo documents Ids in NodeJs using @elastic/enterprise-search


In my NodeJs API, I haven't been able to query a meta engine that is composed by multiple single engines. I need to get documents filtered by a list of Ids.

I'm using @elastic/enterprise-search package version 8.6.0

When I try listing all the documents it works, I get all of them. But I haven't been able to get by ids.

I tried using the client method getDocuments(), but it doesn't find anything and returns a list of nulls. I try something like this but still get errors:

const { Client } = require('@elastic/enterprise-search');

const client = () =>
    new Client({
        url: process.env.ELASTIC_SEARCH_URL,
        auth: {
            token: process.env.ELASTIC_SEARCH_KEY,
        },
    });

const searchItemsFromMetaEngineAppSearch = async (engine, idList) => {
    const requests = idList.map((id) => {
        return {
            index: engine,
            body: {
                query: {
                    ids: {
                        values: [id],
                    },
                },
            },
        };
    });

    const documents = await client().app.search({
        body: requests,
    });
    return documents;
};

There's not much info in the web about querying meta engines.


Solution

  • Turns out that the ids of elements in a meta engine are not the same as elements in a single engine.

    In a single engine you'd do something like this:

    const documents = await client().app.getDocuments({
         engineName: engine, 
         ids: [ "id" ]
    });
    

    In a meta engine you have to use the "scoped id" ("{engine}|{id}") of the element to get by id, using name of the engine to which they belong:

    const documents = await client().app.getDocuments({
         engineName: engine, 
         ids: [ "engineName|id" ]
    });
    

    (https://www.elastic.co/guide/en/app-search/current/meta-engines-guide.html#meta-engines-guide-scoped-ids)