Goja Bleve JavaScript API reference

Reference for the `require("bleve")` module, including mapping builders, query builders, search requests, indexes, batches, vectors, and result objects.

Sections

Terminology & Glossary
πŸ“– Documentation
Navigation
5 sectionsv0.1
πŸ“„ Goja Bleve JavaScript API reference β€” glaze help goja-bleve-js-api-reference
goja-bleve-js-api-reference

Goja Bleve JavaScript API reference

Reference for the `require("bleve")` module, including mapping builders, query builders, search requests, indexes, batches, vectors, and result objects.

Topicgoja-bleveblevejavascriptapireferencevector-searchgoja-bleveevalrunreploutput

This reference describes the JavaScript surface exposed by require("bleve"). The API wraps Bleve's Go types in opaque JavaScript objects and uses builders for mappings, queries, search requests, and indexes.

The reference is written for script authors. It focuses on what each function returns, what the object is used for, and the lifecycle rules that matter when a script runs inside a generated xgoja binary.

Module import

const bleve = require("bleve");

All functions below are properties of that module object.

Top-level factories

FunctionReturnsPurpose
bleve.mapping() / bleve.indexMapping()MappingBuilderBuild an index mapping.
bleve.docMapping() / bleve.documentMapping()DocumentMappingBuilderBuild a document mapping.
bleve.field()FieldBuilderBuild a field mapping.
bleve.memory()IndexBuilderBuild an in-memory index.
bleve.create(path)IndexBuilderBuild a new persistent index at path.
bleve.open(path)IndexBuilderOpen an existing persistent index at path.
bleve.search() / bleve.searchRequest()SearchRequestBuilderBuild a search request.
bleve.bool()BoolQueryBuild a mutable Boolean query.
bleve.match(text)QueryBuilderBuild an analyzed match query.
bleve.matchPhrase(text)QueryBuilderBuild an analyzed phrase query.
bleve.term(value)QueryBuilderBuild an exact term query.
bleve.prefix(prefix)QueryBuilderBuild a prefix query.
bleve.wildcard(pattern)QueryBuilderBuild a wildcard query.
bleve.regexp(pattern)QueryBuilderBuild a regular-expression query.
bleve.fuzzy(term)QueryBuilderBuild a fuzzy term query.
bleve.queryString(query)QueryBuild a query from Bleve query-string syntax.
bleve.matchAll()QueryMatch every document.
bleve.matchNone()QueryMatch no documents; useful for pure KNN.
bleve.conj(...queries) / bleve.conjunction(...queries)QueryCombine queries with AND.
bleve.disj(...queries) / bleve.disjunction(...queries)QueryCombine queries with OR.

MappingBuilder

Create with bleve.mapping() or bleve.indexMapping().

const mapping = bleve.mapping()
  .defaultMapping(docMapping)
  .defaultField("body")
  .defaultAnalyzer("standard")
  .build();

Methods:

MethodReturnsDescription
.defaultMapping(mapping)thisSets the mapping used for documents without a type-specific mapping.
.addTypeMapping(name, mapping)thisAdds a named type mapping.
.typeMapping(name, mapping)thisAlias for .addTypeMapping.
.typeField(name)thisSets the document field used to choose type mappings.
.defaultAnalyzer(name)thisSets the analyzer name used when a field does not override it.
.defaultField(name)thisSets the field used by queries that do not specify a field.
.storeDynamic(enabled)thisControls dynamic field storage.
.indexDynamic(enabled)thisControls dynamic field indexing.
.docValuesDynamic(enabled)thisControls dynamic doc values.
.build()MappingProduces the opaque mapping wrapper.

Use build() once the mapping is complete. Pass the resulting Mapping to an IndexBuilder.

DocumentMappingBuilder

Create with bleve.docMapping() or bleve.documentMapping().

const doc = bleve.docMapping()
  .field("title", bleve.field().text())
  .field("status", bleve.field().keyword())
  .dynamic(false)
  .build();

Methods:

MethodReturnsDescription
.field(name, field)thisAdds a field mapping. Accepts a FieldBuilder or built FieldMapping.
.addField(name, field)thisAlias for .field.
.subDocument(name, doc)thisAdds a nested document mapping.
.addSubDoc(name, doc)thisAlias for .subDocument.
.dynamic(enabled)thisEnables or disables dynamic mapping for this document.
.enabled(enabled)thisEnables or disables this document mapping.
.nested(enabled)thisMarks the document mapping as nested where supported by Bleve.
.defaultAnalyzer(name)thisSets the analyzer for fields under this document mapping.
.build()DocumentMappingProduces the opaque document mapping wrapper.

FieldBuilder

Create with bleve.field().

Field type methods:

MethodPurpose
.text()Analyzed full-text field.
.keyword()Exact keyword field.
.number()Numeric field.
.datetime()Date/time field.
.boolean()Boolean field.
.geoPoint()Geographic point field.
.geoShape()Geographic shape field.
.ip()IP address field.
.disabled()Field is present in documents but not indexed.
.vector(dims)Numeric vector field with dims dimensions.
.vectorBase64(dims)Base64-encoded vector field with dims dimensions.

Common field options:

MethodPurpose
.name(name)Sets the explicit field name.
.analyzer(name)Sets the analyzer for text fields.
.store(enabled)Stores field values for result retrieval.
.index(enabled)Enables/disables indexing.
.docValues(enabled)Enables doc values.
.includeTermVectors(enabled)Stores term vectors for highlighting/explanation use cases.
.includeInAll(enabled)Includes the field in Bleve's all-field behavior where applicable.
.dateFormat(name)Sets date parser/format name for datetime fields.
.similarity(name)Sets vector similarity; common values include cosine, dot_product, and l2_norm.
.optimizedFor(name)Sets vector optimization preference; common values include recall, latency, and memory-efficient.
.build()Produces a FieldMapping.

Vector methods are available in the API, but vector execution requires a binary built with vector support.

Query and QueryBuilder

Most query factories return a QueryBuilder. A query builder supports:

MethodReturnsDescription
.field(name)thisRestricts the query to a field.
.boost(value)thisSets query boost.

Query objects are consumed by SearchRequestBuilder.query(...), Boolean queries, conjunctions, and disjunctions.

Examples:

const text = bleve.match("semantic search").field("body").boost(2);
const exact = bleve.term("published").field("status");
const phrase = bleve.matchPhrase("hybrid retrieval").field("title");
const parsed = bleve.queryString("title:bleve +status:published");

BoolQuery

Create with bleve.bool().

MethodReturnsDescription
.addMust(...queries)thisAdds required clauses.
.addShould(...queries)thisAdds optional/scoring clauses.
.addMustNot(...queries)thisAdds prohibited clauses.
.field(name)thisInherited query field helper where applicable.
.boost(value)thisInherited query boost helper where applicable.

Example:

const query = bleve.bool()
  .addMust(bleve.match("search").field("body"))
  .addShould(bleve.match("goja").field("title"))
  .addMustNot(bleve.term("archived").field("status"));

SearchRequestBuilder

Create with bleve.search() or bleve.searchRequest().

MethodReturnsDescription
.query(query)thisSets the main Bleve query.
.size(n)thisSets the number of hits to return.
.from(n)thisSets result offset.
.fields(names)thisRequests stored fields by name.
.sort(names)thisSets sort expressions, for example ["-_score"] or ["-publishedAt"].
.highlight(fields, style)thisEnables highlighting for fields.
.explain(enabled)thisEnables score explanations.
.score(mode)thisSets score fusion mode: default, none, rrf, or rsf.
.scoreRankConstant(n)thisSets RRF rank constant.
.scoreWindowSize(n)thisSets fusion candidate window size.
.knnOperator(operator)thisSets KNN operator: or or and.
.knn(field, vector, k, boost)thisAdds a KNN clause. Vector can be number[], Float32Array, or Float64Array.
.build()SearchRequestProduces the opaque search request wrapper.

Examples:

const request = bleve.search()
  .query(bleve.match("privacy").field("body"))
  .size(10)
  .fields(["title", "status"])
  .build();

Pure KNN:

const request = bleve.search()
  .query(bleve.matchNone())
  .knn("embedding", [0.1, 0.2, 0.3, 0.4], 5, 1.0)
  .build();

Hybrid RRF:

const request = bleve.search()
  .query(bleve.match("migration").field("text"))
  .knn("embedding", embedding, 20, 1.0)
  .score("rrf")
  .scoreRankConstant(60)
  .scoreWindowSize(50)
  .build();

IndexBuilder

Create with bleve.memory(), bleve.create(path), or bleve.open(path).

MethodReturnsDescription
.mapping(mapping)thisSets the mapping for a new in-memory or persistent index.
.name(name)thisSets a diagnostic index name where supported.
.build()IndexOpens or creates the index.

For open(path), the mapping is already part of the existing index and normally should not be supplied.

Index

MethodReturnsDescription
.index(id, doc)voidAdds or replaces a document.
.delete(id)voidDeletes a document by ID.
.search(request)SearchResultExecutes a built search request.
.docCount()numberReturns the indexed document count.
.newBatch() / .batch()BatchCreates a new batch bound to this index.
.close()voidCloses the index.

Batch

MethodReturnsDescription
.index(id, doc)thisQueues an index operation.
.delete(id)thisQueues a delete operation.
.size()numberReturns the approximate batch size.
.operationCount()numberReturns queued operation count.
.reset()thisClears queued operations before execution.
.execute()voidSubmits the batch and marks it executed.

After .execute() succeeds, later mutation or reset calls throw an error. Create a fresh batch for additional work.

SearchResult and SearchHit

index.search(request) returns:

interface SearchResult {
  total: number;
  maxScore: number;
  took: string;
  hits: SearchHit[];
}

interface SearchHit {
  id: string;
  score: number;
  fields: Record<string, unknown>;
  fragments?: unknown;
  locations?: unknown;
  sort?: unknown[];
  explanation?: unknown;
  scoreBreakdown?: unknown;
}

The exact optional properties depend on the request and Bleve's response. Request stored fields with .fields([...]) if you need document values in hit.fields.

TypeScript declaration snapshot

The repository tests a TypeScript declaration snapshot in pkg/testdata/bleve.d.ts.golden. Treat that file as a compact machine-checkable API summary. Update it whenever public JavaScript APIs change.

Troubleshooting

ProblemCauseSolution
A method is missing from a builderThe object is already built or is the wrong builder type.Keep builder variables separate from built wrapper variables.
field() or mapping() accepts an object but later failsA plain JavaScript object was passed where an opaque goja-bleve wrapper was required.Use objects returned by this module's builders only.
Vector arrays are rejectedThe value is not a supported vector representation or has the wrong dimensions.Use number[], Float32Array, or Float64Array, and match the mapped dimension count.
Result fields are absentFields were not stored or were not requested.Set .store(true) on the field mapping and call .fields([...]) in the request.
Batch reuse failsThe batch has already been executed.Create a new batch from the index.

See Also

  • goja-bleve-getting-started
  • goja-bleve-user-guide