在ElasticSearch中使用must和should
我有以下格式的数据:
{
"name": "a school",
"type": "school", // Either school, university or dorm
"city": "Konya",
"district": "Selçuklu",
"dorm_type": null,
"dorm_gender": null,
"school_type": "private", // Either public or private or mix
"uni_type": null,
},
{
"name": "some univeristy",
"type": "university", // Either school, university or dorm
"city": "Istanbul",
"district": "Besiktas",
"dorm_type": null,
"dorm_gender": null,
"school_type": "null",
"uni_type": "private", // Either public or private
},
{
"name": "some dormitory",
"type": "dormitory", // Either school, university or dormitory
"city": "Istanbul",
"district": "Besiktas",
"dorm_type": "private", // Either public or private
"dorm_gender": "boys", // Either boys or girls
"school_type": "null",
"uni_type": "null",
}
{
"size": 500,
"from": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "`*q*`",
"fields": [
"name"
]
}
},
{
"match": {
"city": "Istanbul"
}
},
{
"match": {
"district": "Beşiktaş"
}
},
{
"bool": {
"should": [
{
"match": {
"dorm_type": "public"
}
},
{
"match": {
"dorm_gender": "boys"
}
},
{
"match": {
"school_type": "private"
}
},
{
"match": {
"uni_type": "public"
}
}
]
}
}
]
}
}
}
如果我希望搜索显示一项或多项过滤,我的查询是否正确? 该查询只是一个示例。我尝试同时使用 must 和 should,因为 must 表示 AND,并且这对于显示城市或地区是有效的。但对于学校、大学和宿舍类型和性别,您只填写其中一项,其他为空。例如,我可能会搜索仅显示公立大学或可能显示公立大学和公共宿舍。 我有点困惑。
I have a data in format below:
{
"name": "a school",
"type": "school", // Either school, university or dorm
"city": "Konya",
"district": "Selçuklu",
"dorm_type": null,
"dorm_gender": null,
"school_type": "private", // Either public or private or mix
"uni_type": null,
},
{
"name": "some univeristy",
"type": "university", // Either school, university or dorm
"city": "Istanbul",
"district": "Besiktas",
"dorm_type": null,
"dorm_gender": null,
"school_type": "null",
"uni_type": "private", // Either public or private
},
{
"name": "some dormitory",
"type": "dormitory", // Either school, university or dormitory
"city": "Istanbul",
"district": "Besiktas",
"dorm_type": "private", // Either public or private
"dorm_gender": "boys", // Either boys or girls
"school_type": "null",
"uni_type": "null",
}
{
"size": 500,
"from": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "`*q*`",
"fields": [
"name"
]
}
},
{
"match": {
"city": "Istanbul"
}
},
{
"match": {
"district": "Beşiktaş"
}
},
{
"bool": {
"should": [
{
"match": {
"dorm_type": "public"
}
},
{
"match": {
"dorm_gender": "boys"
}
},
{
"match": {
"school_type": "private"
}
},
{
"match": {
"uni_type": "public"
}
}
]
}
}
]
}
}
}
Is my query correct if I want search to show one or more than one filteration?
The query is just an example. I tried to use both must and should because must means AND and this is valid to show city or district. But for school, university and dorm types and gender you only have one of them filled and others are null. For instance I may search for showing only public universities or maybe public universities and public dorms.
I am kind of confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必须和应该是 AND
must and should is AND