在ElasticSearch中使用must和should

发布于 2025-01-11 10:13:50 字数 2087 浏览 2 评论 0原文

我有以下格式的数据:

{
   "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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

如果我希望搜索显示一项或多项过滤,我的查询是否正确? 该查询只是一个示例。我尝试同时使用 mustshould,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2025-01-18 10:13:50

必须和应该是 AND

  • 查询必须是 AND
  • 查询应该是 OR

must and should is AND

  • query in must is AND
  • query in should is OR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文