UNPKG

advanced-search-library

Version:

Intelligent search library with typo correction, autocomplete, and flexible data structure support

281 lines (221 loc) β€’ 7.93 kB
# πŸ” Advanced Search Library - Intelligent Search with Typo Correction An advanced search library featuring typo correction, autocomplete, and flexible data structure support with optimized results ranking. ## ✨ Features - πŸš€ **High Performance**: Millisecond search response times - πŸ”§ **Typo Correction**: "cofee" β†’ "coffee", "phoen" β†’ "phone" - πŸ“ **Autocomplete**: Smart suggestions after 3+ characters - πŸ“Š **Field-based Priority**: Intelligent scoring based on name, title, description, etc. - πŸ”„ **Flexible Data Structure**: Compatible with any JSON data format - 🎯 **Auto Field Detection**: Automatically analyzes and prioritizes data structure - πŸ” **Advanced Filtering**: Price, brand, category-based filtering - πŸ’Ύ **Search History**: Search history management and analytics - πŸ“± **Modern UI**: Responsive and user-friendly interface - 🌐 **Universal**: Works in Node.js and browsers ## πŸš€ Quick Start ```javascript const AdvancedSearch = require('./src/AdvancedSearch'); // Create search engine (compatible with any data structure) const search = new AdvancedSearch({ // You can specify custom fields customFields: [ { field: 'name', priority: 10, exact: 20 }, { field: 'description', priority: 5, exact: 10 }, { field: 'tags', priority: 7, exact: 14 } ] }); // Add data - any structure works search.addData([ { id: 1, name: "Coffee Machine", category: "Appliances", price: 299, tags: ["coffee", "automatic"] }, { id: 2, title: "Tea Cup Set", description: "Glass cup set", price: 89 }, { id: 3, name: "Drum Set", category: "Music", price: 1299, keywords: ["instrument"] } ]); // Search with field-based priority scoring const results = search.search("cofee"); // Corrected to "coffee" console.log(results); ``` ## πŸ“– API Documentation ### Constructor Options | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `maxResults` | number | 50 | Maximum number of results | | `minQueryLength` | number | 1 | Minimum query length for search | | `typoThreshold` | number | 2 | Maximum edit distance for typo correction | | `historyLimit` | number | 100 | Maximum search history entries | | `customFields` | array | auto-detected | Custom field configuration | | `autoDetectFields` | boolean | true | Automatically detect fields from data | ### Custom Field Configuration ```javascript const search = new AdvancedSearch({ customFields: [ { field: 'name', priority: 15, exact: 25 }, // High priority for exact matches { field: 'title', priority: 15, exact: 25 }, { field: 'description', priority: 8, exact: 12 }, { field: 'tags', priority: 10, exact: 18 }, { field: 'category', priority: 12, exact: 20 } ] }); ``` ### Main Methods #### `addData(items)` Adds data to the search index. Accepts single item or array. ```javascript search.addData([ { id: 1, name: "Product 1", category: "Electronics" }, { id: 2, title: "Product 2", type: "Books" } ]); ``` #### `search(query, filters = {})` Performs search with optional filters. ```javascript const results = search.search("laptop", { category: "Electronics", priceRange: [500, 2000], minRating: 4.0, sortBy: "price_asc" }); ``` #### `autocomplete(query)` Returns autocomplete suggestions. ```javascript const suggestions = search.autocomplete("lap"); // ["laptop", "lamp", ...] ``` ### Filter Options ```javascript const filters = { category: "Electronics", // Category filter priceRange: [100, 500], // Price range [min, max] brand: "Apple", // Brand filter minRating: 4.0, // Minimum rating sortBy: "price_asc" // Sort options }; ``` ### Sort Options - `relevance` (default) - By search relevance - `price_asc` / `price_desc` - By price - `name_asc` / `name_desc` - By name - `rating_desc` - By rating - `newest` - By creation date ## 🎯 Advanced Usage ### Auto Field Detection The library automatically detects and prioritizes fields in your data: ```javascript const search = new AdvancedSearch(); // Different data structures work automatically search.addData([ { productName: "Phone", specs: "Smart device", manufacturer: "Apple" }, { title: "Book", content: "Programming guide", author: "John Doe" }, { name: "Shirt", details: "Cotton fabric", brand: "Nike" } ]); // Library automatically detects: productName, title, name as high-priority fields ``` ### Performance Optimization ```javascript // Test with 1000+ items const results = search.search("product"); // Average search time: ~10ms for 1000 items ``` ### Multi-language Support ```javascript const search = new AdvancedSearch({ typoMap: { // Add your language-specific typo corrections 'computer': 'computer', 'compter': 'computer', 'komputr': 'computer' } }); ``` ## πŸ“Š Examples ### Basic Usage ```bash npm run example-basic ``` ### Advanced Features ```bash npm run example-advanced ``` ### Flexible Data Structures ```bash npm run example-flexible ``` ### Web Demo ```bash npm run demo # Then open demo/index.html in your browser ``` ## πŸ§ͺ Testing ```bash npm test ``` **Test Results**: 16/16 tests passing βœ… ## πŸ“ˆ Performance Benchmarks | Dataset Size | Search Time | Memory Usage | |--------------|-------------|--------------| | 100 items | 0.5ms | ~2MB | | 1,000 items | 5ms | ~15MB | | 10,000 items | 45ms | ~120MB | ## πŸ”§ Configuration Examples ### E-commerce Setup ```javascript const ecommerceSearch = new AdvancedSearch({ customFields: [ { field: 'name', priority: 20, exact: 40 }, { field: 'brand', priority: 15, exact: 30 }, { field: 'category', priority: 12, exact: 24 }, { field: 'description', priority: 8, exact: 16 }, { field: 'tags', priority: 10, exact: 20 } ] }); ``` ### Blog/Content Setup ```javascript const blogSearch = new AdvancedSearch({ customFields: [ { field: 'title', priority: 25, exact: 50 }, { field: 'summary', priority: 15, exact: 30 }, { field: 'content', priority: 5, exact: 10 }, { field: 'tags', priority: 12, exact: 24 }, { field: 'author', priority: 8, exact: 16 } ] }); ``` ## πŸ› οΈ Browser Usage ```html <script src="src/AdvancedSearch.js"></script> <script> const search = new AdvancedSearch(); search.addData(yourData); const results = search.search("query"); </script> ``` ## πŸ“„ License MIT License - see [LICENSE](LICENSE) file for details. ## πŸ‘¨β€πŸ’» Developer **RΔ±dvan Sevindik** - GitHub: [@Ridvan0](https://github.com/Ridvan0) - LinkedIn: [ridvansevindik](https://www.linkedin.com/in/ridvansevindik/) - Email: sevindikbusiness@gmail.com ## πŸ”— Links - **GitHub Profile:** [https://github.com/Ridvan0](https://github.com/Ridvan0) - **GitHub Repository:** [https://github.com/Ridvan0/turkish-search](https://github.com/Ridvan0/turkish-search) - **Issues:** [Report a bug](https://github.com/Ridvan0/turkish-search/issues) - **LinkedIn:** [RΔ±dvan Sevindik](https://www.linkedin.com/in/ridvansevindik/) ## 🌟 Contributing 1. Fork the repository 2. Create your feature branch (`git checkout -b feature/amazing-feature`) 3. Commit your changes (`git commit -m 'Add amazing feature'`) 4. Push to the branch (`git push origin feature/amazing-feature`) 5. Open a Pull Request ## πŸ“ Changelog ### v1.1.0 - βœ… Flexible data structure support - βœ… Auto field detection - βœ… Enhanced relevance scoring - βœ… Multi-word search improvements - βœ… Performance optimizations ### v1.0.0 - βœ… Initial release - βœ… Basic search functionality - βœ… Typo correction - βœ… Autocomplete - βœ… Filtering and sorting