summaryrefslogtreecommitdiff
path: root/test/elixir/test/nouveau_test.exs
blob: 3bea874d9b96c687492bd603c6998b46e98da84b (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
defmodule NouveauTest do
  use CouchTestCase

  @moduletag :search

  @moduledoc """
  Test search
  """

  def create_search_docs(db_name) do
    resp = Couch.post("/#{db_name}/_bulk_docs",
      headers: ["Content-Type": "application/json"],
      body: %{:docs => [
                %{"_id" => "doc4", "foo" => "foo", "bar" => 42},
                %{"_id" => "doc3", "foo" => "bar", "bar" => 12.0},
                %{"_id" => "doc1", "foo" => "baz", "bar" => 0},
                %{"_id" => "doc2", "foo" => "foobar", "bar" => 100},
      ]}
    )
    assert resp.status_code in [201]
  end

  def create_partitioned_search_docs(db_name) do
    resp = Couch.post("/#{db_name}/_bulk_docs",
      headers: ["Content-Type": "application/json"],
      body: %{:docs => [
                %{"_id" => "foo:doc4", "foo" => "foo", "bar" => 42},
                %{"_id" => "bar:doc3", "foo" => "bar", "bar" => 12.0},
                %{"_id" => "foo:doc1", "foo" => "baz", "bar" => 0},
                %{"_id" => "bar:doc2", "foo" => "foobar", "bar" => 100},
      ]}
    )
    assert resp.status_code in [201]
  end

  def create_ddoc(db_name, opts \\ %{}) do
    default_ddoc = %{
      nouveau: %{
        bar: %{
          default_analyzer: "standard",
          index: """
            function (doc) {
              index("string", "foo", doc.foo, {store: true});
              index("double", "bar", doc.bar, {store: true});
            }
          """
        }
      }
    }

    ddoc = Enum.into(opts, default_ddoc)

    resp = Couch.put("/#{db_name}/_design/foo", body: ddoc)
    assert resp.status_code in [201]
    assert Map.has_key?(resp.body, "ok") == true
  end

  def create_mango_index(db_name) do
    body = %{
      type: "nouveau",
      index: %{
        fields: [
          %{name: "foo", type: "string"},
          %{name: "bar", type: "number"}
        ]
      }
    }

    resp = Couch.post("/#{db_name}/_index", body: body)
    assert resp.status_code in [200]
  end

  def get_ids(resp) do
    %{:body => %{"hits" => hits}} = resp
    Enum.map(hits, fn hit -> hit["doc"]["_id"] end)
  end

  def get_mango_ids(resp) do
    %{:body => %{"docs" => docs}} = resp
    Enum.map(docs, fn doc -> doc["_id"] end)
  end

  def get_bookmark(resp) do
    %{:body => %{"bookmark" => bookmark}} = resp
    bookmark
  end

  def assert_status_code(resp, code) do
    assert resp.status_code == code,
      "status code: #{resp.status_code}, resp body: #{:jiffy.encode(resp.body)}"
  end

  test "search analyze", context do
    url = "/_nouveau_analyze"
    resp = Couch.post(url,
      headers: ["Content-Type": "application/json"],
      body: %{analyzer: "standard", text: "hello there"})
    assert_status_code(resp, 200)
    assert resp.body ==  %{"tokens" => ["hello", "there"]}
  end

  @tag :with_db
  test "search info", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    # query it so it builds
    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)

    url = "/#{db_name}/_design/foo/_nouveau_info/bar"
    resp = Couch.get(url)
    assert_status_code(resp, 200)
    info = Map.get(resp.body, "search_index")
    assert Map.get(info, "disk_size") > 0
    assert Map.get(info, "num_docs") > 0
    assert Map.get(info, "update_seq") > 0
  end

  @tag :with_db
  test "search returns all items for GET", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    # nouveau sorts by _id as tie-breaker
    assert ids == ["doc1", "doc2", "doc3", "doc4"]
  end

  @tag :with_db
  test "search returns all items for POST", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc1", "doc2", "doc3", "doc4"]
  end

  @tag :with_db
  test "search returns all items (paginated)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"

    # page 1
    resp = Couch.post(url, body: %{q: "*:*", limit: 2, include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc1", "doc2"]

    # page 2
    resp = Couch.post(url, body: %{q: "*:*", limit: 2, bookmark: get_bookmark(resp), include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc3", "doc4"]
  end

  @tag :with_db
  test "search for foo:bar", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "foo:bar", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc3"]
  end

  @tag :with_db
  test "sort by string field (asc)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", sort: "foo<string>", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc3", "doc1", "doc4", "doc2"]
  end

  @tag :with_db
  test "sort by string field (desc)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", sort: "-foo<string>", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc2", "doc4", "doc1", "doc3"]
  end

  @tag :with_db
  test "sort by numeric field (asc)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", sort: "bar<double>", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc1", "doc3", "doc4", "doc2"]
  end

  @tag :with_db
  test "sort by numeric field (desc)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", sort: "-bar<double>", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["doc2", "doc4", "doc3", "doc1"]
  end

  @tag :with_db
  test "counts", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", counts: ["foo"], include_docs: true})
    assert_status_code(resp, 200)
    %{:body => %{"counts" => counts}} = resp
    assert counts == %{"foo" => %{"bar" => 1, "baz" => 1, "foo" => 1, "foobar" => 1}}
  end

  @tag :with_db
  test "ranges", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", ranges: %{bar: [
      %{label: "cheap", min: 0, max: 42},
      %{label: "expensive", min: 42, min_inclusive: false, max: 1000}]},
      include_docs: true})
    assert_status_code(resp, 200)
    %{:body => %{"ranges" => ranges}} = resp
    assert ranges == %{"bar" => %{"cheap" => 3, "expensive" => 1}}
  end

  @tag :with_db
  test "ranges (open)", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", ranges: %{bar: [
      %{label: "cheap", max: 42},
      %{label: "expensive", min: 42, min_inclusive: false}]},
      include_docs: true})
    assert_status_code(resp, 200)
    %{:body => %{"ranges" => ranges}} = resp
    assert ranges == %{"bar" => %{"cheap" => 3, "expensive" => 1}}
  end

  @tag :with_db
  test "mango search by number", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_mango_index(db_name)

    url = "/#{db_name}/_find"
    resp = Couch.post(url, body: %{selector: %{bar: %{"$gt": 5}}})
    assert_status_code(resp, 200)
    ids = get_mango_ids(resp)
    assert ids == ["doc2", "doc3", "doc4"]
  end

  @tag :with_db
  test "mango search by string", context do
    db_name = context[:db_name]
    create_search_docs(db_name)
    create_mango_index(db_name)

    url = "/#{db_name}/_find"
    resp = Couch.post(url, body: %{selector: %{foo: %{"$eq": "foo"}}})
    assert_status_code(resp, 200)
    ids = get_mango_ids(resp)
    assert ids == ["doc4"]
  end

  @tag :with_partitioned_db
  test "search GET (partitioned)", context do
    db_name = context[:db_name]
    create_partitioned_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_partition/foo/_design/foo/_nouveau/bar"
    resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["foo:doc1", "foo:doc4"]

    url = "/#{db_name}/_partition/bar/_design/foo/_nouveau/bar"
    resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["bar:doc2", "bar:doc3"]
  end

  @tag :with_partitioned_db
  test "search POST (partitioned)", context do
    db_name = context[:db_name]
    create_partitioned_search_docs(db_name)
    create_ddoc(db_name)

    url = "/#{db_name}/_partition/foo/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["foo:doc1", "foo:doc4"]

    url = "/#{db_name}/_partition/bar/_design/foo/_nouveau/bar"
    resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
    assert_status_code(resp, 200)
    ids = get_ids(resp)
    assert ids == ["bar:doc2", "bar:doc3"]
  end

  @tag :with_partitioned_db
  test "mango (partitioned)", context do
    db_name = context[:db_name]
    create_partitioned_search_docs(db_name)
    create_mango_index(db_name)

    url = "/#{db_name}/_partition/foo/_find"
    resp = Couch.post(url, body: %{selector: %{foo: %{"$eq": "foo"}}})
    assert_status_code(resp, 200)
    ids = get_mango_ids(resp)
    assert ids == ["foo:doc4"]

    url = "/#{db_name}/_partition/bar/_find"
    resp = Couch.post(url, body: %{selector: %{foo: %{"$eq": "bar"}}})
    assert_status_code(resp, 200)
    ids = get_mango_ids(resp)
    assert ids == ["bar:doc3"]
  end

end