advanced-search-library
Version:
Intelligent search library with typo correction, autocomplete, and flexible data structure support
281 lines (221 loc) β’ 7.93 kB
Markdown
# π 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