redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.91k stars 1.89k forks source link

Add an object field name transformer to clean up results from RediSearch over JSON searches #1729

Open simonprickett opened 2 years ago

simonprickett commented 2 years ago

When we perform a search over JSON document content, it would be nice to provide an extra object parameter describing how to name fields in the objects returned...

Example:

let results = await client.json.get('noderedis:jsondata', {
    path: ['.pets[1].name', '.pets[1].age']
});

might return:

{ '.pets[1].name': 'Rex', '.pets[1].age': 3 }

Proposing that something like this:

let results = await client.json.get('noderedis:jsondata', {
    path: ['.pets[1].name', '.pets[1].age'],
    responseMap: {
        '.pets[1].name': 'name',
        '.pets[1].age': 'age'
    }
});

would return:

{ 'name': 'Rex', 'age': 3 }
ljluestc commented 2 weeks ago

const redis = require('redis');

async function getJsonData(client, key, options) {
    // Fetch JSON data from Redis
    const rawResults = await client.json.get(key, { path: options.path });

    // Check if responseMap exists and process the results
    if (options.responseMap) {
        const mappedResults = {};

        for (const [originalKey, newKey] of Object.entries(options.responseMap)) {
            if (rawResults[originalKey] !== undefined) {
                mappedResults[newKey] = rawResults[originalKey];
            }
        }

        return mappedResults; // Return the mapped results
    }

    return rawResults; // Return raw results if no responseMap is provided
}

// Usage example
(async () => {
    const client = redis.createClient();

    try {
        await client.connect();

        const results = await getJsonData(client, 'noderedis:jsondata', {
            path: ['.pets[1].name', '.pets[1].age'],
            responseMap: {
                '.pets[1].name': 'name',
                '.pets[1].age': 'age'
            }
        });

        console.log(results); // Expected output: { name: 'Rex', age: 3 }
    } catch (err) {
        console.error('Error fetching JSON data:', err);
    } finally {
        await client.quit();
    }
})();
ljluestc commented 2 weeks ago

const redis = require('redis');

async function getJsonData(client, key, options) {
    // Fetch JSON data from Redis
    const rawResults = await client.json.get(key, { path: options.path });

    // Check if responseMap exists and process the results
    if (options.responseMap) {
        const mappedResults = {};

        for (const [originalKey, newKey] of Object.entries(options.responseMap)) {
            if (rawResults[originalKey] !== undefined) {
                mappedResults[newKey] = rawResults[originalKey];
            }
        }

        return mappedResults; // Return the mapped results
    }

    return rawResults; // Return raw results if no responseMap is provided
}

// Usage example
(async () => {
    const client = redis.createClient();

    try {
        await client.connect();

        const results = await getJsonData(client, 'noderedis:jsondata', {
            path: ['.pets[1].name', '.pets[1].age'],
            responseMap: {
                '.pets[1].name': 'name',
                '.pets[1].age': 'age'
            }
        });

        console.log(results); // Expected output: { name: 'Rex', age: 3 }
    } catch (err) {
        console.error('Error fetching JSON data:', err);
    } finally {
        await client.quit();
    }
})();