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
use anyhow::{bail, Context, Result};
use elasticsearch::{
http::transport::{CloudConnectionPool, SingleNodeConnectionPool, TransportBuilder},
indices::{IndicesCreateParts, IndicesExistsParts},
Elasticsearch, IndexParts,
};
use serde::Serialize;
use serde_json::json;
use crate::{WikipediaDocument, WikipediaNamespace};
pub struct ElasticHelper {
pub client: Elasticsearch,
pub index_name: String,
}
impl ElasticHelper {
pub fn new<S: Into<String>>(
es_settings: &merino_settings::ElasticsearchSettings,
index_name: S,
) -> Result<Self> {
let transport_builder = match &es_settings.connection {
merino_settings::ElasticsearchConnection::Single { url } => {
TransportBuilder::new(SingleNodeConnectionPool::new(
elasticsearch::http::Url::parse(&url.to_string())
.context("Could not parse Elasticsearch URL")?,
))
}
merino_settings::ElasticsearchConnection::Cloud { cloud_id } => TransportBuilder::new(
CloudConnectionPool::new(cloud_id)
.context("Could not create Elasticsearch cloud connection")?,
),
merino_settings::ElasticsearchConnection::None => {
bail!("Elasticsearch is not configured")
}
};
let transport = transport_builder
.build()
.context("misconfigured elasticsearch")?;
let client = Elasticsearch::new(transport);
Ok(Self {
client,
index_name: index_name.into(),
})
}
pub async fn index_exists(&self) -> Result<bool> {
let exists_res = self
.client
.indices()
.exists(IndicesExistsParts::Index(&[&self.index_name]))
.send()
.await
.context(format!("index_exists({}) request", self.index_name))?;
match exists_res.status_code().as_u16() {
200 => Ok(true),
404 => Ok(false),
code => {
let exists_output = exists_res
.text()
.await
.context("Fetching text of exists command")?;
Err(anyhow::anyhow!(
"Unexpected status code from index_exists({}) {code}. Error: {exists_output}",
self.index_name
))
}
}
}
pub async fn index_create(&self) -> Result<()> {
let create_res = self
.client
.indices()
.create(IndicesCreateParts::Index(&self.index_name))
.body(json!({
"mappings": {
"_source": {
"excludes": ["page_text"],
},
"properties": {
"title": {"type": "text", "store": true, "analyzer": "english"},
"page_text": {"type": "search_as_you_type", "store": false, "analyzer": "english"},
"url": {"type": "keyword", "index": false, "store": true},
"namespace_id": {"type": "keyword", "index": false},
"page_id": {"type": "keyword", "index": false},
}
}
}))
.send()
.await
.context(format!("index_create({}) request", self.index_name))?;
let status = create_res.status_code();
if status.is_success() {
Ok(())
} else {
let exists_output = create_res
.text()
.await
.context("Fetching text of exists command")?;
Err(anyhow::anyhow!(
"Unexpected status code from index_create({}) {}. Error: {exists_output}",
self.index_name,
status.as_u16(),
))
}
}
pub async fn index_ensure_exists(&self) -> Result<()> {
if !self.index_exists().await? {
self.index_create().await?;
}
Ok(())
}
pub async fn doc_add<T>(&self, doc: T) -> Result<()>
where
T: HelperIndexable,
{
let doc_id = doc.doc_id();
self.client
.index(IndexParts::IndexId(&self.index_name, &doc_id))
.body(doc)
.send()
.await
.context("indexing doc request")?;
Ok(())
}
}
pub trait HelperIndexable: Serialize {
fn doc_id(&self) -> String;
}
impl HelperIndexable for WikipediaDocument {
fn doc_id(&self) -> String {
format!(
"wikidoc/{}/{}",
<i32 as From<WikipediaNamespace>>::from(self.namespace),
self.page_id
)
}
}