Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Search Text Transformer Laravel Package

becklyn/search-text-transformer

Converts HTML into clean, searchable plain text for indexing with search engines like Elasticsearch. Use SearchTextTransformer to strip tags and normalize content, making page text suitable for full‑text search and highlighting.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Search Pipeline Synergy: Perfectly aligns with Laravel’s search workflows (e.g., Scout, custom Elasticsearch integrations) by acting as a pre-processing layer to sanitize HTML before indexing. Reduces noise in search indexes (e.g., scripts, styles) and improves relevance.
  • Event-Driven Potential: Ideal for Laravel’s observer pattern (e.g., saved events on models) or queue-based async processing (e.g., search-text-transform:worker). Supports both real-time (e.g., API responses) and batch (e.g., nightly re-indexing) use cases.
  • Extensibility: Lightweight core allows for customization (e.g., preserving headings, ignoring boilerplate) via composition or forking. Can be wrapped in a service interface for testability and dependency injection.
  • Hybrid Content Systems: Useful for dual-storage architectures (e.g., raw HTML for rendering + transformed text for search) or headless CMS integrations (e.g., Strapi, Craft CMS).

Integration Feasibility

  • Laravel Native: Zero friction with:
    • Service Container: Bind SearchTextTransformer to an interface (e.g., HtmlToTextTransformer) for mocking in tests.
    • Facades: Create a SearchText facade for concise usage (e.g., SearchText::transform($html)).
    • Artisan Commands: Add search:text-transform for CLI batch processing.
  • Search Backend Compatibility:
    • Elasticsearch: Integrate via:
      • Ingest Pipelines (custom processor for pre-indexing).
      • Application-Side Transformation (before sending documents to Elasticsearch).
    • Algolia: Use in pre-indexing scripts or Laravel middleware.
    • Scout: Override toSearchableArray() to include transformed text.
  • Legacy Systems: Bridges HTML-heavy databases (e.g., MySQL LONGTEXT fields) to modern search backends with minimal refactoring.

Technical Risk

Risk Mitigation
HTML Parsing Limitations Supplement with DOMDocument for edge cases (e.g., nested tables, malformed markup).
Performance Bottlenecks Benchmark with large payloads (e.g., 1MB HTML); cache transformed results if reprocessing is common.
Maintenance Risk (Last Release: 2022) Fork the repo under your org’s GitHub; monitor for PHP 8.x compatibility issues.
Search Relevance Degradation A/B test transformed vs. raw HTML in search results; adjust rules if needed.
Dependency Conflicts Verify compatibility with Laravel’s PHP version (tested on 8.x); no heavy dependencies.

Key Questions

  1. Use Case Specificity:
    • Is this for real-time search (e.g., autocomplete) or batch indexing (e.g., nightly updates)?
    • Should transformed text be stored persistently (e.g., in a searchable_text column) or used transiently?
  2. Customization Needs:
    • Does the default transformation (e.g., stripping all tags) meet requirements, or are selective rules needed (e.g., preserve <h1><h3>)?
    • Should transformed text include metadata (e.g., author, publish date) for enriched search?
  3. Scalability:
    • What’s the volume of HTML to process (e.g., 100 docs/day vs. 1M/docs/day)? Plan queue workers accordingly.
    • Are there parallelization opportunities (e.g., by content type or priority)?
  4. Failure Handling:
    • How should malformed HTML be handled (e.g., skip, log, or transform partially)?
    • What’s the rollback strategy if transformation fails mid-pipeline (e.g., database transaction)?
  5. Testing Strategy:
    • Should the package’s test suite be extended with domain-specific fixtures (e.g., product pages, articles)?
    • How will search quality be validated post-integration (e.g., user feedback, query logs)?

Integration Approach

Stack Fit

  • Laravel Integration:
    • Service Provider: Register the transformer as a singleton or bound interface in AppServiceProvider:
      $this->app->bind(HtmlToTextTransformer::class, function ($app) {
          return new SearchTextTransformer();
      });
      
    • Facade: Create app/Facades/SearchText.php for cleaner usage:
      SearchText::transform($html);
      
    • Artisan Command: Add app/Console/Commands/TransformSearchText.php for batch processing:
      php artisan search:text-transform --model=Post --batch=100
      
  • Search Backend:
    • Elasticsearch: Use as a custom ingest pipeline processor or application-side transformer:
      $transformedText = app(HtmlToTextTransformer::class)->transform($post->body);
      $elasticsearch->index('posts', $post->id, ['text' => $transformedText]);
      
    • Scout: Override toSearchableArray():
      public function toSearchableArray()
      {
          return [
              'body' => SearchText::transform($this->body),
              // other fields...
          ];
      }
      
    • Algolia: Transform in a pre-indexing script or middleware:
      $client->saveObject([
          'objectID' => $post->id,
          'text' => SearchText::transform($post->body),
      ]);
      
  • Queue Workers:
    • Offload transformation to Laravel queues (e.g., search-text-transform:worker) for async processing:
      // In a model observer or event listener
      TransformSearchTextJob::dispatch($post);
      
    • Use batch jobs (e.g., Laravel Nova batches) for large datasets.

Migration Path

  1. Phase 1: Proof of Concept (1–2 Days)
    • Integrate the transformer in a staging environment.
    • Test with a subset of HTML content (e.g., 100 documents).
    • Validate transformed text meets search relevance (e.g., no truncated content, preserved keywords).
    • Benchmark performance (e.g., transformation time for 1KB vs. 100KB HTML).
  2. Phase 2: Pipeline Integration (3–5 Days)
    • Option A (Batch): Set up a cron job to transform and re-index existing content:
      * 3 * * * php artisan search:text-transform --model=Post --batch=500
      
    • Option B (Real-Time): Hook into model events (e.g., saved) or API routes:
      // In PostObserver.php
      public function saved(Post $post)
      {
          TransformSearchTextJob::dispatch($post);
      }
      
  3. Phase 3: Scaling and Optimization (Ongoing)
    • Optimize queue workers for parallel processing (e.g., Laravel Horizon).
    • Implement caching for frequently transformed content (e.g., Redis):
      $cacheKey = "search_text:{$post->id}";
      $transformedText = cache()->remember($cacheKey, now()->addHours(1), function () use ($post) {
          return SearchText::transform($post->body);
      });
      
    • Monitor performance metrics (e.g., avg. transformation time) and adjust batch sizes.

Compatibility

  • Laravel Versions: Tested on PHP 8.x; compatible with Laravel 9/10 (no breaking changes expected).
  • HTML Parsing: Defaults to simple string replacement; supplement with DOMDocument for complex cases (e.g., nested tables).
  • Search Backends: No hard dependencies, but may require custom mappings (e.g., Elasticsearch keyword vs. text fields).
  • Existing Code: Minimal changes if using dependency injection; facade/artisan command adds convenience without refactoring.

Sequencing

  1. Pre-Indexing Workflow:
    Raw HTML → [SearchTextTransformer] → Clean Text → [Elasticsearch/Algolia Indexer]
    
  2. Real-Time Workflow:
    Model Save → [Event Listener] → [SearchTextTransformer] → Update Search Index
    
  3. Hybrid Workflow:
    • Transform on write (for new/updated content) via queue jobs.
    • Batch-transform legacy content via cron (prioritize high-value collections first).

Operational Impact

Maintenance

  • Dependencies: Minimal (only PHP); no external services to monitor.
  • Updates: Monitor for upstream releases (last update in 2022).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor