/** * Query DSL type definitions for type-safe Elasticsearch queries */ /** * Elasticsearch query types */ export type QueryType = 'match' | 'match_phrase' | 'multi_match' | 'term' | 'terms' | 'range' | 'exists' | 'prefix' | 'wildcard' | 'regexp' | 'fuzzy' | 'ids' | 'bool' | 'match_all' | 'query_string' | 'simple_query_string'; /** * Boolean query clause types */ export type BoolClause = 'must' | 'should' | 'must_not' | 'filter'; /** * Sort order */ export type SortOrder = 'asc' | 'desc'; /** * Match query operator */ export type MatchOperator = 'or' | 'and'; /** * Multi-match type */ export type MultiMatchType = 'best_fields' | 'most_fields' | 'cross_fields' | 'phrase' | 'phrase_prefix' | 'bool_prefix'; /** * Range query bounds */ export interface RangeBounds { gt?: number | string | Date; gte?: number | string | Date; lt?: number | string | Date; lte?: number | string | Date; } /** * Match query definition */ export interface MatchQuery { match: { [field: string]: { query: string; operator?: MatchOperator; fuzziness?: number | 'AUTO'; boost?: number; }; }; } /** * Match phrase query definition */ export interface MatchPhraseQuery { match_phrase: { [field: string]: { query: string; slop?: number; boost?: number; }; }; } /** * Multi-match query definition */ export interface MultiMatchQuery { multi_match: { query: string; fields: string[]; type?: MultiMatchType; operator?: MatchOperator; boost?: number; }; } /** * Term query definition */ export interface TermQuery { term: { [field: string]: { value: string | number | boolean; boost?: number; }; }; } /** * Terms query definition */ export interface TermsQuery { terms: { [field: string]: Array | number | undefined; }; } /** * Range query definition */ export interface RangeQuery { range: { [field: string]: RangeBounds & { boost?: number; }; }; } /** * Exists query definition */ export interface ExistsQuery { exists: { field: string; }; } /** * Prefix query definition */ export interface PrefixQuery { prefix: { [field: string]: { value: string; boost?: number; }; }; } /** * Wildcard query definition */ export interface WildcardQuery { wildcard: { [field: string]: { value: string; boost?: number; }; }; } /** * Regexp query definition */ export interface RegexpQuery { regexp: { [field: string]: { value: string; flags?: string; boost?: number; }; }; } /** * Fuzzy query definition */ export interface FuzzyQuery { fuzzy: { [field: string]: { value: string; fuzziness?: number | 'AUTO'; boost?: number; }; }; } /** * IDs query definition */ export interface IdsQuery { ids: { values: string[]; }; } /** * Match all query definition */ export interface MatchAllQuery { match_all: { boost?: number; }; } /** * Query string query definition */ export interface QueryStringQuery { query_string: { query: string; default_field?: string; fields?: string[]; default_operator?: MatchOperator; boost?: number; }; } /** * Simple query string query definition */ export interface SimpleQueryStringQuery { simple_query_string: { query: string; fields?: string[]; default_operator?: MatchOperator; boost?: number; }; } /** * Boolean query definition */ export interface BoolQuery { bool: { must?: QueryDSL[]; should?: QueryDSL[]; must_not?: QueryDSL[]; filter?: QueryDSL[]; minimum_should_match?: number | string; boost?: number; }; } /** * Union of all query types */ export type QueryDSL = MatchQuery | MatchPhraseQuery | MultiMatchQuery | TermQuery | TermsQuery | RangeQuery | ExistsQuery | PrefixQuery | WildcardQuery | RegexpQuery | FuzzyQuery | IdsQuery | MatchAllQuery | QueryStringQuery | SimpleQueryStringQuery | BoolQuery; /** * Sort field definition */ export interface SortField { [field: string]: { order?: SortOrder; mode?: 'min' | 'max' | 'sum' | 'avg' | 'median'; missing?: '_first' | '_last' | string; }; } /** * Search request options */ export interface SearchOptions { /** Query to execute */ query?: QueryDSL; /** Fields to return (source filtering) */ fields?: string[]; /** Exclude source fields */ excludeFields?: string[]; /** Number of results to return */ size?: number; /** Offset for pagination */ from?: number; /** Sort order */ sort?: Array; /** Track total hits */ trackTotalHits?: boolean | number; /** Search timeout */ timeout?: string; /** Highlight configuration */ highlight?: { fields: { [field: string]: { pre_tags?: string[]; post_tags?: string[]; fragment_size?: number; number_of_fragments?: number; }; }; }; /** Aggregations */ aggregations?: Record; } /** * Aggregation types */ export type AggregationType = 'terms' | 'avg' | 'sum' | 'min' | 'max' | 'cardinality' | 'stats' | 'extended_stats' | 'percentiles' | 'date_histogram' | 'histogram' | 'range' | 'filter' | 'filters' | 'nested' | 'reverse_nested' | 'top_hits'; /** * Terms aggregation */ export interface TermsAggregation { terms: { field: string; size?: number; order?: Record; missing?: string | number; }; aggs?: Record; } /** * Metric aggregations (avg, sum, min, max, cardinality) */ export interface MetricAggregation { avg?: { field: string; missing?: number; }; sum?: { field: string; missing?: number; }; min?: { field: string; missing?: number; }; max?: { field: string; missing?: number; }; cardinality?: { field: string; }; } /** * Stats aggregation */ export interface StatsAggregation { stats: { field: string; }; } /** * Extended stats aggregation */ export interface ExtendedStatsAggregation { extended_stats: { field: string; }; } /** * Percentiles aggregation */ export interface PercentilesAggregation { percentiles: { field: string; percents?: number[]; }; } /** * Date histogram aggregation */ export interface DateHistogramAggregation { date_histogram: { field: string; calendar_interval?: string; fixed_interval?: string; format?: string; time_zone?: string; min_doc_count?: number; }; aggs?: Record; } /** * Histogram aggregation */ export interface HistogramAggregation { histogram: { field: string; interval: number; min_doc_count?: number; }; aggs?: Record; } /** * Range aggregation */ export interface RangeAggregation { range: { field: string; ranges: Array<{ from?: number; to?: number; key?: string; }>; }; aggs?: Record; } /** * Filter aggregation */ export interface FilterAggregation { filter: QueryDSL; aggs?: Record; } /** * Top hits aggregation */ export interface TopHitsAggregation { top_hits: { size?: number; sort?: Array; _source?: boolean | { includes?: string[]; excludes?: string[]; }; }; } /** * Union of all aggregation types */ export type AggregationDSL = TermsAggregation | MetricAggregation | StatsAggregation | ExtendedStatsAggregation | PercentilesAggregation | DateHistogramAggregation | HistogramAggregation | RangeAggregation | FilterAggregation | TopHitsAggregation; /** * Search result hit */ export interface SearchHit { _index: string; _id: string; _score: number | null; _source: T; fields?: Record; highlight?: Record; sort?: Array; } /** * Aggregation bucket */ export interface AggregationBucket { key: string | number; key_as_string?: string; doc_count: number; [aggName: string]: unknown; } /** * Terms aggregation result */ export interface TermsAggregationResult { doc_count_error_upper_bound: number; sum_other_doc_count: number; buckets: AggregationBucket[]; } /** * Metric aggregation result */ export interface MetricAggregationResult { value: number; } /** * Stats aggregation result */ export interface StatsAggregationResult { count: number; min: number; max: number; avg: number; sum: number; } /** * Percentiles aggregation result */ export interface PercentilesAggregationResult { values: Record; } /** * Generic aggregation result */ export type AggregationResult = TermsAggregationResult | MetricAggregationResult | StatsAggregationResult | PercentilesAggregationResult | { buckets: AggregationBucket[]; } | { value: number; } | Record; /** * Search result */ export interface SearchResult { took: number; timed_out: boolean; _shards: { total: number; successful: number; skipped: number; failed: number; }; hits: { total: { value: number; relation: 'eq' | 'gte'; }; max_score: number | null; hits: SearchHit[]; }; aggregations?: Record; }