|
| 1 | +import { Kuzzle, WebSocket } from "kuzzle-sdk"; |
| 2 | + |
| 3 | +const kuzzle = new Kuzzle(new WebSocket("localhost")); |
| 4 | +const index = "nyc-open-data"; |
| 5 | +const collection = "green-taxi"; |
| 6 | +const mappings = { |
| 7 | + dynamic: "false" as const, |
| 8 | + properties: { |
| 9 | + name: { |
| 10 | + type: "keyword", |
| 11 | + }, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +beforeAll(async () => { |
| 16 | + await kuzzle.connect(); |
| 17 | + if (await kuzzle.index.exists(index)) { |
| 18 | + await kuzzle.index.delete(index); |
| 19 | + } |
| 20 | + |
| 21 | + await kuzzle.index.create(index); |
| 22 | + await kuzzle.collection.create(index, collection, { |
| 23 | + mappings, |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +afterAll(async () => { |
| 28 | + await kuzzle.index.delete(index); |
| 29 | + kuzzle.disconnect(); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("collection:update", () => { |
| 33 | + it("should reindex the collection if asked to", async () => { |
| 34 | + await kuzzle.document.create( |
| 35 | + index, |
| 36 | + collection, |
| 37 | + { age: 42, name: "Bob" }, |
| 38 | + "document-1", |
| 39 | + { refresh: "wait_for" }, |
| 40 | + ); |
| 41 | + |
| 42 | + let result = await kuzzle.document.search(index, collection, { |
| 43 | + query: { |
| 44 | + range: { |
| 45 | + age: { |
| 46 | + gte: 40, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + expect(result.hits.length).toEqual(0); |
| 53 | + |
| 54 | + await kuzzle.collection.update(index, collection, { |
| 55 | + mappings: { properties: { age: { type: "long" } } }, |
| 56 | + reindexCollection: true, |
| 57 | + }); |
| 58 | + |
| 59 | + // Wait for the reindexing to complete |
| 60 | + await new Promise((r) => setTimeout(r, 2000)); |
| 61 | + |
| 62 | + result = await kuzzle.document.search(index, collection, { |
| 63 | + query: { |
| 64 | + range: { |
| 65 | + age: { |
| 66 | + gte: 40, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(result.hits.length).toEqual(1); |
| 73 | + }); |
| 74 | +}); |
0 commit comments