xLucene Query Translator
raw JSON →The `xlucene-translator` package is a core component within the Teraslice monorepo, specifically designed to convert xLucene syntax queries into various target database query formats. This provides a standardized query language layer, abstracting the complexities of different data backend syntaxes. The current stable version, derived from the Teraslice monorepo, is v3.6.1, with frequent updates that align with the active development cadence of the larger Teraslice project. Its primary differentiation lies in offering a flexible and unified translation interface, which is crucial for applications that need to query data across heterogeneous data stores, such as Elasticsearch or OpenSearch. The package is built for Node.js environments (requiring Node.js >=22.0.0) and ships with comprehensive TypeScript type definitions, enhancing developer experience and type safety.
Common errors
error Error: Unknown dialect: 'mongoDB' ↓
error Error: Invalid xLucene query syntax near 'my_field:badly-formatted:' ↓
error TypeError: Cannot read properties of undefined (reading 'type') at Translator.translate ↓
fieldMapping object, and that each entry includes a valid type property (e.g., { type: 'keyword' }). Warnings
breaking The `xlucene-translator` package, consistent with its parent Teraslice monorepo, now requires Node.js version 22.0.0 or higher. Running with older Node.js versions will result in environment errors or unpredictable behavior. ↓
gotcha Providing an incorrect or incomplete field mapping configuration to the `Translator` constructor can lead to failed translations or logically incorrect database queries, especially for type-sensitive operations like range queries or full-text searches. ↓
gotcha As a component of the Teraslice monorepo, `xlucene-translator`'s versioning and release cycle are tightly coupled with the broader Teraslice project. Independent updates to this package might introduce compatibility issues if not aligned with the main Teraslice release strategy. ↓
Install
npm install xlucene-translator yarn add xlucene-translator pnpm add xlucene-translator Imports
- Translator wrong
const Translator = require('xlucene-translator');correctimport { Translator } from 'xlucene-translator'; - QueryConfig
import { QueryConfig } from 'xlucene-translator'; - translate wrong
import translate from 'xlucene-translator';correctimport { translate } from 'xlucene-translator';
Quickstart
import { Translator } from 'xlucene-translator';
interface MyDocumentMapping {
field1: { type: 'keyword' };
age: { type: 'integer' };
isActive: { type: 'boolean' };
}
// Example field mapping, mimicking an Elasticsearch index mapping
const fieldMapping: MyDocumentMapping = {
field1: { type: 'keyword' },
age: { type: 'integer' },
isActive: { type: 'boolean' },
};
async function runTranslation() {
try {
// Initialize the translator for the 'elasticsearch' dialect
// The fieldMapping is essential for correct type inference and query generation
const translator = new Translator<MyDocumentMapping>('elasticsearch', fieldMapping);
const xluceneQuery1 = 'field1:"value example" AND age:>25 AND isActive:true';
const translatedQuery1 = translator.translate(xluceneQuery1);
console.log('xLucene Query 1:', xluceneQuery1);
console.log('Translated Elasticsearch Query 1:', JSON.stringify(translatedQuery1, null, 2));
const xluceneQuery2 = 'field1:test* OR age:[10 TO 20]';
const translatedQuery2 = translator.translate(xluceneQuery2);
console.log('\n-----------------------------------\n');
console.log('xLucene Query 2:', xluceneQuery2);
console.log('Translated Elasticsearch Query 2:', JSON.stringify(translatedQuery2, null, 2));
} catch (error: any) {
console.error('Translation failed:', error.message);
}
}
runTranslation();