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?