简体   繁体   English

elasticsearch golang 的多字段搜索查询

[英]multi fields search query for elasticsearch golang

I have a situation where I need to do elastic search based on multi-field.我有一种情况需要基于多字段进行弹性搜索。 For Example: I have multiple fields in my postindex and I want to apply condition on four these fields (ie userid , channelid , createat , teamid ) to meet my search requirement.例如:我的postindex中有多个字段,我想对这四个字段(即useridchannelidcreateatteamid )应用条件以满足我的搜索要求。 When value of all these fields matched then search query displays results and if one of these is not match with values in postindex then it display no result.当所有这些字段的值都匹配时,搜索查询会显示结果,如果其中一个字段与postindex中的值不匹配,则不会显示任何结果。

I am trying to make a multifield search query for go-elasticsearch to search data from my post index .我正在尝试为 go-elasticsearch 进行多字段搜索查询,以从我post index中搜索数据。 For the searcquery result four field must match otherwise it display 0 hit/no-result.对于搜索查询结果,四个字段必须匹配,否则它会显示 0 个命中/无结果。

So, I think you need to write a following query:因此,我认为您需要编写以下查询:

GET postindex/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "userid": {
                    "value": "mcqmycxpyjrddkie9mr13txaqe"
                  }
                }
              },
              {
                "term": {
                  "channelid": {
                    "value": "dnoihmrinins3qrm6bb9175ume"
                  }
                }
              },
              {
                "range": {
                  "createat": {
                    "gt": 1672909114890
                  }
                }
              }
            ]
          }
        },
        {
          "term": {
            "teamid": {
              "value": "qomrg11o8b8ijxoy8hrcnweoay"
            }
          }
        }
      ]
    }
  }
}

In here, there is a bool query with should in parent scope, which is like OR.在这里,有一个 bool 查询 with should in parent scope,类似于 OR。 And inside the should there is another bool query with must which is like AND.在 should 内部还有另一个带有 must 的 bool 查询,类似于 AND。 We can also write the query shorter, but this will be better for you to understand.我们也可以将查询写得更短一些,但这会让您更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM