如何使用 Elastic Search for Node.js 搜索属性?



我正在将各种文档摄取到弹性搜索中,如下所示:

await client.index({
id: url,
index: 'docs',
body: {
'Url': url,
'Accessed': new Date().toISOString(),
'Content': content,
'Title': title,
}
});

现在,我只想对_source.Title进行搜索。这应该是对英文文本的模糊搜索。

如何使用弹性搜索库实现此目的?

const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
// What goes here?
}
},
});

我正在使用"@elastic/elasticsearch": "^7.4.0".

有很多方法可以解决这个问题。但最初我会使用模糊的match查询:

const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
"match" : {
"title" : {
"query" : "some title terms",
"fuzziness": 2
}
}
}
},
});

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
"fuzzy": {
"title": {
"value": "{searchQuery}"
}
}
}
},
});

最新更新