summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 615e4ed4c924d4fae198eaa187f747be07c0838f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

use serde::{Serialize, Deserialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiRequest {
    pub method: String,
    pub path_and_query: String,
    pub body: Option<SearchBody>,
}

/*

URL query parameters:

    pub allow_no_indices: Option<bool>,
    pub allow_partial_search_results: Option<bool>,
    pub batched_reduce_size: Option<u32>,
    pub ccs_minimize_roundtrips: Option<bool>,
    pub docvalue_fields: Option<String>, // array of strings, comma-separated
    pub expand_wildcards: Option<String>,
    pub explain: Option<bool>,
    pub from: Option<u32>,
    pub ignore_throttled: Option<bool>,
    pub ignore_unavailable: Option<bool>,
    pub max_concurrent_shard_requests: Option<u32>,
    pub pre_filter_shard_size: Option<u32>,
    pub preference: Option<String>,
    pub q: Option<String>
    pub request_cache: Option<bool>,
    pub rest_total_hits_as_int: Option<bool>,
    pub routing: Option<String>,
    pub scroll: Option<String>, // string is "time value"
    pub search_type: Option<String>,
    pub seq_no_primary_term: Option<bool>,
    pub size: Option<u32>,
    pub sort: Option<String>, // array of strings, comma-separated
    pub _source: Option<bool>, // TODO: bool or string
    pub _source_excludes: Option<String>, // array of strings, comma-separated
    pub _source_includes: Option<String>, // array of strings, comma-separated
    pub stats: Option<String>,
    pub stored_fields: Option<String>, // array of strings, comma-separated
    pub suggest_field: Option<String>,
    pub suggest_text: Option<String>,
    pub terminate_after: Option<u32>,
    pub timeout: Option<String>, // string is "time units"
    pub track_scores: Option<bool>,
    pub track_total_hits: Option<bool>, // XXX: bool or integer
    pub typed_keys: Option<bool>,
    pub version: Option<bool>,
*/

// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
#[derive(Serialize, Deserialize, Debug)]
pub struct SearchBody {
    pub query: Option<ApiQuery>,
    pub highlight: Option<ApiHighlight>,
    pub collapse: Option<ApiCollapse>,
    pub post_filter: Option<ApiQuery>, // TODO: leaf query only?
    pub rescore: Option<ApiRescore>, // TODO: single or an array of rescore objects
    // script_fields disabled

    // https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html
    // pub sort: Option<Vec<SortThing>>, // XXX: array of object or string

    pub slice: Option<ApiSlice>,
    pub stored_fields: Option<String>, // array of strings, or "_none_"

    // overlap with URL query parameters
    pub docvalue_fields: Option<Vec<String>>, // XXX: array of strings or objects? {field, format} in the objects
    pub explain: Option<bool>,
    pub from: Option<u32>,
    pub min_score: Option<f64>,
    pub seq_no_primary_term: Option<bool>,
    pub size: Option<u32>,
    pub _source: Option<bool>, // XXX: bool, string, or object
    pub terminate_after: Option<u32>,
    pub timeout: Option<String>, // string is "time units"

    // not enumerated in direct docs, but seems to be allowed?
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiSlice {
    id: u32,
    max: u32,
    field: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiRescore{
    pub query: Option<ApiQuery>,
    pub window_size: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum ApiQuery {
    // compound queries
    #[serde(rename = "bool")]
    Bool(HashMap<String, QueryFieldOrString>),
    #[serde(rename = "boosting")]
    Boosting {positive: Box<ApiQuery>, negative: Box<ApiQuery>, negative_boost: f64},
    #[serde(rename = "constant_score")]
    ConstantScore {filter: Box<ApiQuery>, boost: Option<f64>},

    // fulltext (leaf) queries

    // term-level (leaf) queries
    #[serde(rename = "match")]
    Match(HashMap<String, QueryFieldOrString>),
    #[serde(rename = "match_phrase")]
    MatchPhrase(HashMap<String, QueryFieldOrString>),
    #[serde(rename = "query_string")]
    QueryString(QueryField),

    // other
    #[serde(rename = "nested")]
    Nested(NestedQuery),
    #[serde(rename = "rescore_query")]
    Rescore(Box<ApiQuery>),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiHighlight{
    // TODO: fields could also be an array of strings?
    fields: HashMap<String, HighlightField>,

    #[serde(flatten)]
    settings: HighlightField,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum QueryFieldOrString {
    Object(QueryField),
    String(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct QueryField{
    query: String,
    fuzziness: Option<String>,
    slop: Option<u32>,
    boost: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NestedQuery{
    path: String,
    query: Box<ApiQuery>,
    score_mode: Option<String>,
    ignore_unmapped: Option<bool>,
}

// https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html
#[derive(Serialize, Deserialize, Debug)]
pub struct HighlightField{
    boundary_chars: Option<String>,
    boundary_max_scan: Option<u32>,
    boundary_scanner: Option<String>,
    boundary_scanner_locale: Option<String>,
    encoder: Option<String>,
    force_source: Option<bool>,
    fragmenter: Option<String>,
    fragment_offset: Option<u32>,
    fragment_size: Option<u32>,
    highlight_query: Option<ApiQuery>,
    matched_fields: Option<Vec<String>>,
    no_match_size: Option<u32>,
    number_of_fragments: Option<u32>,
    order: Option<String>,
    phrase_limit: Option<u32>,
    pre_tags: Option<Vec<String>>,
    post_tags: Option<Vec<String>>,
    require_field_match: Option<bool>,
    tags_schema: Option<String>,
    #[serde(rename = "type")]
    highlight_type: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiCollapse{
    field: String,
    inner_hits: Option<InnerHits>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct InnerHits {
    from: Option<u32>,
    size: Option<u32>,
    sort: Option<Vec<String>>,
    name: Option<String>,
}