0

我的索引数据是

    {
      "first_name":"Kevin",
      "last_name":"John",
      "job": "IT"
    }
    {
      "first_name":"John",
      "last_name":"Thimothy",
      "job": "Accountant"
    }
    {
      "first_name":"Eric",
      "last_name":"Villa",
      "job": "Driver"
    }
    {
      "first_name":"John",
      "last_name":"Villa",
      "job": "Student"
    }

我不确定是否有人可以帮助我构建一个查询以获取具有first_namelast_name作为John和工作为IT或的数据Student

4

1 回答 1

2

您需要使用bool/must/should子句的组合

搜索查询:

    {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "first_name": "John"
                    }
                  },
                  {
                    "match": {
                      "last_name": "John"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "job": "IT"
                    }
                  },
                  {
                    "match": {
                      "job": "student"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }

搜索结果将是

    "hits": [
          {
            "_index": "66982646",
            "_type": "_doc",
            "_id": "1",
            "_score": 2.4079456,
            "_source": {
              "first_name": "Kevin",
              "last_name": "John",
              "job": "IT"
            }
          },
          {
            "_index": "66982646",
            "_type": "_doc",
            "_id": "4",
            "_score": 1.89712,
            "_source": {
              "first_name": "John",
              "last_name": "Villa",
              "job": "Student"
            }
          }
        ]
于 2021-04-07T08:59:03.063 回答