ElasticSearch 7.x exact match

Hello,
I am trying to implement an exact match for a term (a url) and I get 0 hits for the following implementation. I am doing this with node, with the latest npm package ("@elastic/elasticsearch": "^7.5.0")

Mapping:
const client = require('./index');
const index = "history";
const type = "pageDataToUser";

async function createIndex(index) {
  try {
    await client.indices.create({ index });
    console.log(`Created index ${index}`);
  } catch (err) {
    console.error(`An error occurred while creating the index ${index}:`);
    console.error(err);
  }
}

async function setQuotesMapping () {
  try {
    const schema = {
      userId: {
        type: "keyword",
      },
      url: {
        type: 'keyword',
      },
      pageTitle: {
        type: 'text',
      },
      pageText: {
        type: 'text',
      }
    };

    await client.indices.putMapping({
      index,
      type,
      include_type_name: true,
      body: {
        properties: schema
      }
    })

    console.log("Quotes mapping created successfully");
  } catch (err) {
    console.error("An error occurred while setting the quotes mapping:");
    console.error(err);
  }
}

createIndex(index);

Indexing and querying:

const client = require('./index');

const addOne = async () => {
  try {
    await client.index({
      index: 'history',
      body: {
        url: 'abc/',
        userId: 'anotherTest',
        pageTitle: 'not importan',
        pageText: 'not importan',
        log: [3, 9, 8]
      }
    });
    const test = await client.search({
      index: 'history',
      body: {
        query: {
          term: {
            url: {
              value: 'abc/',
            }
          }
        }
      }
    })
    console.log(test.body.hits, 'body');
    const total = await client.search({
      body: {
        query: {
          match_all: {}
        }
      }
    })
    console.log(total.body.hits.hits, 'this is total');
  } catch (error) {
    console.log(error)
  }
}

addOne();
module.exports = addOne;

This returns 0 hits. However, if the value I index and search for is 'abc', or anything not containg separators (like spaces, or slashes), then this works as expected.
Nevertheless, what I will be storing here are urls, and these will contain special characters. Any idea what I am doing wrong?

Welcome!

I'm not really reading JS code, but here it sounds like you are not calling a refresh before searching. May be that's the reason.

I've added this line in the try block:
await client.indices.refresh(); and I still do not get any results back. Also, the match_all is working, itś only the exact match which doesn't seem to work.

I have managed to find a solution for this. You can find the details here.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.