|
| 1 | +# Turing JS SDK |
| 2 | + |
| 3 | +A TypeScript SDK for the Turing Semantic Navigation Search API. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @openviglet/turing-js-sdk |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +### TypeScript/ES Modules |
| 14 | +```typescript |
| 15 | +import { TurSNSiteSearchService, TurSNFilterQueryOperator } from '@openviglet/turing-js-sdk'; |
| 16 | + |
| 17 | +// Initialize the service |
| 18 | +const searchService = new TurSNSiteSearchService('https://your-turing-instance.com'); |
| 19 | + |
| 20 | +// Perform a basic search |
| 21 | +const results = await searchService.search('yourSiteName', { |
| 22 | + q: 'search query', |
| 23 | + rows: 10, |
| 24 | + currentPage: 1 |
| 25 | +}); |
| 26 | + |
| 27 | +console.log(results); |
| 28 | +``` |
| 29 | + |
| 30 | +### CommonJS |
| 31 | +```javascript |
| 32 | +const { TurSNSiteSearchService, TurSNFilterQueryOperator } = require('@openviglet/turing-js-sdk'); |
| 33 | + |
| 34 | +async function example() { |
| 35 | + const searchService = new TurSNSiteSearchService('https://your-turing-instance.com'); |
| 36 | + |
| 37 | + const results = await searchService.search('yourSiteName', { |
| 38 | + q: 'artificial intelligence', |
| 39 | + rows: 10, |
| 40 | + currentPage: 1, |
| 41 | + fqOperator: TurSNFilterQueryOperator.AND |
| 42 | + }); |
| 43 | + |
| 44 | + console.log('Search results:', results); |
| 45 | +} |
| 46 | + |
| 47 | +example().catch(console.error); |
| 48 | +``` |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +### Basic Configuration |
| 53 | + |
| 54 | +```typescript |
| 55 | +const searchService = new TurSNSiteSearchService('https://your-turing-instance.com'); |
| 56 | +``` |
| 57 | + |
| 58 | +### Advanced Configuration |
| 59 | + |
| 60 | +```typescript |
| 61 | +import axios from 'axios'; |
| 62 | + |
| 63 | +const searchService = new TurSNSiteSearchService('https://your-turing-instance.com', { |
| 64 | + timeout: 5000, |
| 65 | + headers: { |
| 66 | + 'User-Agent': 'MyApp/1.0' |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +// Set authentication |
| 71 | +searchService.setAuth('your-token-here'); |
| 72 | +``` |
| 73 | + |
| 74 | +## API Methods |
| 75 | + |
| 76 | +### Search Methods |
| 77 | + |
| 78 | +#### `search(siteName: string, params?: TurSNSearchParams): Promise<TurSNSiteSearch>` |
| 79 | + |
| 80 | +Perform a GET search request. |
| 81 | + |
| 82 | +```typescript |
| 83 | +const results = await searchService.search('mysite', { |
| 84 | + q: 'artificial intelligence', |
| 85 | + rows: 20, |
| 86 | + currentPage: 1, |
| 87 | + sort: 'relevance', |
| 88 | + fqOperator: TurSNFilterQueryOperator.AND |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +#### `searchPost(siteName: string, postParams: TurSNSitePostParams, params?: TurSNSearchParams): Promise<TurSNSiteSearch>` |
| 93 | + |
| 94 | +Perform a POST search request with advanced parameters. |
| 95 | + |
| 96 | +```typescript |
| 97 | +const results = await searchService.searchPost('mysite', { |
| 98 | + userId: 'user123', |
| 99 | + query: 'machine learning', |
| 100 | + populateMetrics: true, |
| 101 | + targetingRules: ['rule1', 'rule2'] |
| 102 | +}, { |
| 103 | + rows: 15 |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +#### `searchList(siteName: string, params?: TurSNSearchParams): Promise<Set<string>>` |
| 108 | + |
| 109 | +Get a list of search result identifiers. |
| 110 | + |
| 111 | +```typescript |
| 112 | +const resultIds = await searchService.searchList('mysite', { |
| 113 | + q: 'data science', |
| 114 | + rows: 50 |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +### Utility Methods |
| 119 | + |
| 120 | +#### `getLocales(siteName: string): Promise<TurSNSiteLocale[]>` |
| 121 | + |
| 122 | +Get available locales for a site. |
| 123 | + |
| 124 | +```typescript |
| 125 | +const locales = await searchService.getLocales('mysite'); |
| 126 | +``` |
| 127 | + |
| 128 | +#### `getLatestSearches(siteName: string, rows?: number, locale?: string, request?: TurSNSearchLatestRequest): Promise<string[]>` |
| 129 | + |
| 130 | +Get latest search queries. |
| 131 | + |
| 132 | +```typescript |
| 133 | +const latestSearches = await searchService.getLatestSearches('mysite', 10, 'en'); |
| 134 | +``` |
| 135 | + |
| 136 | +## Search Parameters |
| 137 | + |
| 138 | +### TurSNSearchParams |
| 139 | + |
| 140 | +| Parameter | Type | Description | |
| 141 | +|-----------|------|-------------| |
| 142 | +| `q` | `string` | Search query | |
| 143 | +| `currentPage` | `number` | Current page number | |
| 144 | +| `rows` | `number` | Number of results per page | |
| 145 | +| `sort` | `string` | Sort criteria | |
| 146 | +| `filterQueriesDefault` | `string[]` | Default filter queries | |
| 147 | +| `filterQueriesAnd` | `string[]` | AND filter queries | |
| 148 | +| `filterQueriesOr` | `string[]` | OR filter queries | |
| 149 | +| `fqOperator` | `TurSNFilterQueryOperator` | Filter query operator | |
| 150 | +| `fqItemOperator` | `TurSNFilterQueryOperator` | Filter query item operator | |
| 151 | +| `group` | `string` | Group parameter | |
| 152 | +| `autoCorrectionDisabled` | `number` | Auto-correction disabled flag | |
| 153 | +| `localeRequest` | `string` | Requested locale | |
| 154 | + |
| 155 | +### TurSNSitePostParams |
| 156 | + |
| 157 | +Extended parameters for POST requests including: |
| 158 | +- User targeting and personalization |
| 159 | +- Advanced filtering options |
| 160 | +- Metrics population settings |
| 161 | + |
| 162 | +## Response Types |
| 163 | + |
| 164 | +### TurSNSiteSearch |
| 165 | + |
| 166 | +The main search response containing: |
| 167 | +- `pagination`: Pagination information |
| 168 | +- `queryContext`: Query context and metadata |
| 169 | +- `results`: Search results with documents |
| 170 | +- `groups`: Result groupings |
| 171 | +- `widget`: Widget configuration |
| 172 | + |
| 173 | +### TurSNSiteSearchDocument |
| 174 | + |
| 175 | +Individual search result document with: |
| 176 | +- `source`: Document source |
| 177 | +- `elevate`: Elevation flag |
| 178 | +- `metadata`: Document metadata |
| 179 | +- `fields`: Document fields |
| 180 | + |
| 181 | +## Enums |
| 182 | + |
| 183 | +### TurSNFilterQueryOperator |
| 184 | + |
| 185 | +- `AND`: AND operation |
| 186 | +- `OR`: OR operation |
| 187 | +- `NONE`: No operation |
| 188 | + |
| 189 | +### TurSNPaginationType |
| 190 | + |
| 191 | +- `FIRST`: First page |
| 192 | +- `LAST`: Last page |
| 193 | +- `PREVIOUS`: Previous page |
| 194 | +- `NEXT`: Next page |
| 195 | +- `PAGE`: Specific page |
| 196 | + |
| 197 | +## Error Handling |
| 198 | + |
| 199 | +```typescript |
| 200 | +try { |
| 201 | + const results = await searchService.search('mysite', { q: 'test' }); |
| 202 | +} catch (error) { |
| 203 | + if (error.response) { |
| 204 | + // Server responded with error status |
| 205 | + console.error('API Error:', error.response.status, error.response.data); |
| 206 | + } else if (error.request) { |
| 207 | + // No response received |
| 208 | + console.error('Network Error:', error.message); |
| 209 | + } else { |
| 210 | + // Other error |
| 211 | + console.error('Error:', error.message); |
| 212 | + } |
| 213 | +} |
| 214 | +``` |
| 215 | + |
| 216 | +## TypeScript Support |
| 217 | + |
| 218 | +This SDK is built with TypeScript and provides full type definitions for all API responses and parameters. |
| 219 | + |
| 220 | +## License |
| 221 | + |
| 222 | +Apache-2.0 |
| 223 | + |
| 224 | +## Contributing |
| 225 | + |
| 226 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments