gpt4 book ai didi

javascript - 使用带有 post_filter 的 query_string 搜索

转载 作者:行者123 更新时间:2023-11-30 19:41:30 24 4
gpt4 key购买 nike

我正在尝试使用 query_string 和 asterix (query: `${city}*`) 搜索城市,同时还通过 countryId 过滤结果。该查询在不过滤 countryId 的情况下运行良好,但在添加过滤器时它什么也找不到。 Boty citycountryId 被映射为 text

代码:

const elasticsearch = require('elasticsearch');

...

this.client = new elasticsearch.Client({
hosts: [this._serviceUri]
});

...

// The interesting part:
const results = await this.client.search({
index: 'cities',
body: {
query: {
query_string: {
query: `${city}*`,
fields: ['city', 'countryId'],
// type: 'best_fields'
}
},
post_filter: { term: { countryId } }
}
});

如何使用 post_filter 或类似工具正确过滤结果?

更新:这是映射的样子:

mappings: {
_doc: {
properties: {
"city": {type: 'text'},
"countryId": {type: 'text'}
}
}
}

最佳答案

我会这样做,而不求助于 post_filter:

// The interesting part:
const results = await this.client.search({
index: 'cities',
body: {
query: {
bool: {
must: {
query_string: {
query: `${city}*`,
fields: ['city', 'countryId'],
// type: 'best_fields'
}
},
filter: {
term: {
countryId: `${countryId}`,
}
}
}
}
}
});

关于javascript - 使用带有 post_filter 的 query_string 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55341074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com