按字段和值过滤弹性搜索结果
我有一个样本数据,其中包括来自大学、学校和宿舍的数据。 我想在 Elasticsearch 中使用这些数据。在进行过滤时,我按如下方式发送正文:
示例数据:
[
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
},
{
name: 'Maisha elementry school',
school_type: 'private',
location: 'B',
},
{
name: 'istanbul university',
university_type: 'public',
location: 'C',
},
{
name: 'Maisha university',
university_type: 'private',
location: 'D',
},
{
name: 'kbb center of Maisha',
dormitory_type: 'public',
location: 'E',
},
{
name: 'high Maisha dorm',
dormitory_type: 'private',
location: 'F',
},
]
我的查询:
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*Maisha*",
"fields": [
"name"
]
}
},
{
"match": {
"school_type": "public"
}
}
]
}
}
结果:
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
}
这个结果实际上是有道理的,因为它给出了准确的结果,但没有显示宿舍和大学数据,这是主要问题。换句话说,我只希望school_type是public,但为什么它会过滤掉并且不显示大学和宿舍数据?
I have a sample data that includes data from univerities, schools and dormitories.
I want to use this data in Elasticsearch. I send the body as follows when making filteration:
Sample Data:
[
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
},
{
name: 'Maisha elementry school',
school_type: 'private',
location: 'B',
},
{
name: 'istanbul university',
university_type: 'public',
location: 'C',
},
{
name: 'Maisha university',
university_type: 'private',
location: 'D',
},
{
name: 'kbb center of Maisha',
dormitory_type: 'public',
location: 'E',
},
{
name: 'high Maisha dorm',
dormitory_type: 'private',
location: 'F',
},
]
My Query:
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*Maisha*",
"fields": [
"name"
]
}
},
{
"match": {
"school_type": "public"
}
}
]
}
}
Result:
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
}
This result actually makes sence because it gives the exact result but dormitory and university data are not shown which is the main problem. In other words, I only want the school_type to be public but why it filters out and not showing university and dormitory data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎是正确的,只需将
bool
中的must
更改为should
即可。我刚刚索引了您的示例数据并使用下面的查询。示例文档
它返回以下结果,希望您正在寻找相同的输出。
You are almost correct, you just need to change
must
inbool
toshould
. I just indexed your sample data and using below query.Sample documents
it returns below result, hope you are looking for the same output.