npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

elasticsearch-dynamic-search

v1.1.7

Published

Intelligent hybrid search library for Elasticsearch with dynamic weight adjustment, proper noun detection, and contextual analysis

Readme

Elasticsearch Dynamic Search

🧠 Intelligent hybrid search library for Elasticsearch with dynamic weight adjustment, proper noun detection, and contextual analysis

Features

  • 🎯 Smart Search Strategy Detection - Automatically determines optimal search approach based on query characteristics
  • 🌍 Regional Query Analysis - Detects geographical context and adjusts search weights accordingly
  • 🏷️ Advanced Proper Noun Recognition - Multi-strategy entity detection (known entities, capitalization, acronyms, product codes, version numbers)
  • ⚖️ Dynamic Weight Balancing - Context-aware lexical/semantic weight adjustment based on query complexity and intent
  • 📊 Detailed Performance Monitoring - Phase-by-phase search analytics and performance tracking
  • 🔧 Elasticsearch Integration - Ready-to-use with modern Elasticsearch 8.x

Installation

npm install elasticsearch-dynamic-search

Quick Start

const { DynamicSearchEngine } = require('elasticsearch-dynamic-search');
const { Client } = require('@elastic/elasticsearch');

// Initialize Elasticsearch client
const client = new Client({
  node: 'https://localhost:9200',
  auth: {
    apiKey: 'your-api-key'
  }
});

// Define your query templates (with mustache variables)
const queryTemplates = {
  rerank: `{
    "retriever": {
      "text_similarity_reranker": {
        "retriever": {
          "linear": {
            "retrievers": [
              {
                "retriever": {
                  "standard": {
                    "query": {
                      "multi_match": {
                        "query": "{{query}}",
                        "fields": ["title^2", "content"]
                      }
                    }
                  }
                },
                "weight": "{{lexical_weight}}"
              },
              {
                "retriever": {
                  "standard": {
                    "query": {
                      "semantic": {
                        "field": "content_vector",
                        "query": "{{query}}"
                      }
                    }
                  }
                },
                "weight": "{{semantic_weight}}"
              }
            ]
          }
        },
        "field": "title",
        "inference_text": "{{query}}",
        "inference_id": "{{inference_id}}"
      }
    },
    "size": 10
  }`,
  noRerank: `{
    "query": {
      "bool": {
        "should": [
          {
            "multi_match": {
              "query": "{{query}}",
              "fields": ["title^2", "content"],
              "boost": "{{lexical_weight}}"
            }
          },
          {
            "semantic": {
              "field": "content_vector",
              "query": "{{query}}",
              "boost": "{{semantic_weight}}"
            }
          }
        ]
      }
    },
    "size": 10
  }`
};

// Create search engine instance
const searchEngine = new DynamicSearchEngine(client, queryTemplates, {
  indexName: 'your-search-index-*',
  enablePerformanceMonitoring: true,
  enableQueryEnhancement: true,
  enableContextualWeighting: true
});

// Perform intelligent search
async function search() {
  try {
    const results = await searchEngine.search('machine learning algorithms', {
      sessionId: 'user-session-123',
      userAgent: 'Mozilla/5.0...',
      timeOfDay: 14,
      domain: 'technical',
      intent: 'exploratory',
      useRerank: true
    });
    
    console.log('Search Results:', results);
    console.log('Applied Strategy:', results.weights.strategy);
    console.log('Weight Distribution:', {
      lexical: `${Math.round(results.weights.lexicalWeight * 100)}%`,
      semantic: `${Math.round(results.weights.semanticWeight * 100)}%`
    });
  } catch (error) {
    console.error('Search failed:', error);
  }
}

search();

Architecture

The library consists of several modular components:

Core Components

  • DynamicSearchEngine - Main orchestrator that coordinates all components
  • QueryAnalyzer - Analyzes query characteristics and determines search strategy
  • QueryEnhancer - Detects proper nouns, regional context, and query statistics
  • ContextualWeighter - Calculates context-aware weights based on historical data
  • WeightCombiner - Combines analysis results into final lexical/semantic weights
  • QueryBuilder - Constructs Elasticsearch queries from templates and weights
  • PerformanceMonitor - Tracks detailed performance metrics across search phases

Advanced Usage

Using Individual Components

const { 
  QueryAnalyzer, 
  QueryEnhancer, 
  WeightCombiner 
} = require('elasticsearch-dynamic-search');

const analyzer = new QueryAnalyzer();
const enhancer = new QueryEnhancer();
const combiner = new WeightCombiner();

// Analyze a query
const analysis = analyzer.analyzeQuery('Microsoft Office 365 training');
console.log('Query Strategy:', analysis.strategy);

// Enhance query with proper noun detection
const enhancement = enhancer.enhanceQuery('Microsoft Office 365 training');
console.log('Detected Proper Nouns:', enhancement.properNouns.properNouns);
console.log('Detected Region:', enhancement.detectedRegion);

Custom Configuration

const searchEngine = new DynamicSearchEngine(client, queryTemplates, {
  indexName: 'my-index-*',
  enablePerformanceMonitoring: true,
  enableQueryEnhancement: true,
  enableContextualWeighting: true,
  cacheResults: true,
  cacheTTL: 300000, // 5 minutes
  
  // Component-specific options
  queryEnhancer: {
    enableRegionalDetection: true,
    enableProperNounDetection: true
  },
  
  weightCombiner: {
    knowledgeSearchBias: 0.15,
    regionalBias: 0.12,
    longQueryBoost: 0.30,
    properNounLexicalWeight: 0.9
  },
  
  queryBuilder: {
    defaultInferenceId: '.rerank-v1-elasticsearch'
  }
});

Search Strategies

The library automatically detects and applies different strategies based on query characteristics:

  • exact_match - For specific terms that need precise matching
  • entity_focused - When proper nouns are detected (companies, products, etc.)
  • conceptual - For broad, knowledge-seeking queries
  • short_query - Optimized for 1-2 word queries
  • descriptive - For detailed, multi-word descriptive queries
  • complex - For complex queries with multiple concepts
  • regional_semantic_enhanced - When geographical context is detected

Performance Monitoring

Get detailed insights into search performance:

// Get performance statistics
const stats = searchEngine.getStats();
console.log('Total Searches:', stats.totalSearches);
console.log('Average Response Time:', stats.averageResponseTime);
console.log('Strategy Usage:', stats.strategyCounts);

// Clear cache if needed
searchEngine.clearCache();

Query Templates

The library uses mustache-style templates for Elasticsearch queries. Supported variables:

  • {{query}} - The search query text
  • {{lexical_weight}} - Calculated lexical weight (0-10 scale)
  • {{semantic_weight}} - Calculated semantic weight (0-10 scale)
  • {{inference_id}} - Inference endpoint for reranking

Requirements

  • Node.js >= 14.0.0
  • Elasticsearch >= 8.0.0
  • Compatible with Elasticsearch's semantic search and reranking features

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please open an issue on the GitHub repository.