ElasticSearch查询-如何传递查询列表

cvxl0en2  于 2023-06-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(139)

我已经创建了一个索引与10000+文件。以下是其中的示例:

{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f05",
    "_score": 13.977877,
    "_source": {
        "customer_id":10,
        "customer_name": Mike,
        "customer_phone": 1111111111,
        "customer_address": "XYZ"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f71",
    "_score": 12.977861,
    "_source": {
        "customer_id":20,
        "customer_name": Angie,
        "customer_phone": 2222222222,
        "customer_address": "ABC"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f62",
    "_score": 10.978777,
    "_source": {
        "customer_id":30,
        "customer_name": John,
        "customer_phone": 3333333333,
        "customer_address": "PQR"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f54",
    "_score": 11.817877,
    "_source": {
        "customer_id":40,
        "customer_name": Andy,
        "customer_phone": 4444444444,
        "customer_address": "MNO"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f32",
    "_score": 14.457877,
    "_source": {
        "customer_id": 50,
        "customer_name": Nick,
        "customer_phone": 5555555555,
        "customer_address": "CDE"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f21",
    "_score": 16.487877,
    "_source": {
        "customer_id":60,
        "customer_name": Atlas,
        "customer_phone": 6666666666,
        "customer_address": "DFE"
    }
}

我想一次传递多个查询作为json body中的列表,并以列表格式获得结果:例如:->我想同时在搜索条件中传递以下3个查询:

1) customer_id = 10, customer_name = Mike, customer_phone = 1111111111
2) customer_id = 40, customer_name = Andy, customer_phone = 4444444444
3) customer_id = 50, customer_name = Nick, customer_phone = 5555555555

虽然,我可以使用'AND'和'OR'合并这3个查询,如下所示:

{
    "query": {
        "query_string": {
            "query": "(customer_id: 10 AND customer_name: Mike AND customer_phone: 1111111111) OR (customer_id: 40 AND customer_name: Andy AND customer_phone: 4444444444) OR (customer_id: 50 AND customer_name: Nick AND customer_phone: 5555555555)"
        }
    }
}

除了如上所述组合查询之外,是否有其他更好的方法来实现相同的目标(例如将查询作为列表传递)。

ohtdti5x

ohtdti5x1#

您可以合并should和must查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 10
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Mike"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 1111111111
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 50
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Nick"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 5555555555
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 40
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Andy"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 4444444444
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

相关问题