zackiles/elasticsearch-odm

Id's with "-" not found when using findOne.

Litterfeldt opened this issue · 1 comments

ElasticSearch version: 2.4.6
Npm package: elasticsearch-odm@1.1.0

import * as elasticsearch from "elasticsearch-odm";

elasticsearch.connect({
    host: "http://localhost:9200",
    index: "thing",
    syncMapping: false
});

const searchSchema = new elasticsearch.Schema({
    thingId: { type: String },
});

const model = elasticsearch.model("stuff", searchSchema);

const elasticSearchCRUD = async (thingId) => {
    console.log("\nCreating thing with thingId: ", thingId);
    await model.create({ thingId });

    await new Promise(res => setTimeout(res, 1000));
    let thing = await model.findOne({ thingId });
    console.log("\nBefore update");
    console.log(thing);

    await new Promise(res => setTimeout(res, 1000));
    await thing.update({field: "field"});
    thing = await model.findOne({ thingId });
    console.log("\nAfter update");
    console.log(thing);

    await new Promise(res => setTimeout(res, 1000));
    await thing.remove();
    thing = await model.findOne({ thingId });
    console.log("\nAfter remove");
    console.log(thing);
}

const isWorking  = async () => elasticSearchCRUD("6334b9bb");
const notWorking = async () => elasticSearchCRUD("6334b9bb-27d4-43f3-aa45-162d6a497f32");

const run = async () => {
    await isWorking();
    await notWorking();
}

run();
$ ts-node test.ts

Creating thing with thingId:  6334b9bb

Before update
modelInstance {
  thingId: '6334b9bb',
  createdOn: '2018-09-04T09:52:17.156Z',
  updatedOn: '2018-09-04T09:52:17.183Z',
  id: 'AWWj_zkuFA-J_V5PmYxh' }

After update
modelInstance {
  thingId: '6334b9bb',
  createdOn: '2018-09-04T09:52:17.156Z',
  updatedOn: '2018-09-04T09:52:17.183Z',
  id: 'AWWj_zkuFA-J_V5PmYxh' }

After remove
modelInstance {
  thingId: '6334b9bb',
  createdOn: '2018-09-04T09:52:17.156Z',
  updatedOn: '2018-09-04T09:52:19.217Z',
  id: 'AWWj_zkuFA-J_V5PmYxh',
  field: 'field' }

Creating thing with thingId:  6334b9bb-27d4-43f3-aa45-162d6a497f32

Before update
undefined
(node:53475) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'update' of undefined
    at Object.<anonymous> (/Users/litterfeldt/Dev/kaching/v2_workspace/be-catalog/src/test.ts:25:17)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/litterfeldt/Dev/kaching/v2_workspace/be-catalog/src/test.ts:4:58)
    at <anonymous>
(node:53475) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:53475) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

"resolved" by setting up the models before establishing the connection to elastic search that creates the indexes.. The default indexes created are not allowed to be overridden and the default analyser is a tokeniser that strips away the "-".

import * as elasticsearch from "elasticsearch-odm-5";

const searchSchema = new elasticsearch.Schema({
    thingId: { type: String },
});

const model = elasticsearch.model("things", searchSchema);

const elasticSearchCRUD = async (thingId) => {
    console.log("\nCreating thing with thingId: ", thingId);
    await model.create({ thingId });

    await new Promise(res => setTimeout(res, 1000));

    let thing = await model.findOne({ thingId });

    console.log(thing);
}

const isWorking  = async () => elasticSearchCRUD("a");
const notWorking = async () => elasticSearchCRUD("a-27");


const run = async () => {
    await elasticsearch.connect({
        host: "http://localhost:9200",
        index: "my-index",
        logging: true,
    });

    await isWorking();
    await notWorking();
}

run();
$ ts-node src/test.ts

Creating thing with thingId:  a
modelInstance {
  thingId: 'a',
  createdOn: '2018-09-04T15:56:33.169Z',
  updatedOn: '2018-09-04T15:56:33.188Z',
  id: 'AWWlTLgauOXlND3DZB0-' }

Creating thing with thingId:  a-27
modelInstance {
  thingId: 'a-27',
  createdOn: '2018-09-04T15:56:34.445Z',
  updatedOn: '2018-09-04T15:56:34.467Z',
  id: 'AWWlTL0YuOXlND3DZB0_' }