如何在ElasticSearch中编写下面的SQL查询?

eivnm1vs  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(141)
SELECT * FROM products WHERE (stock_code = '1234' AND category_code = '3214') OR (stock_code = '1234' AND category_code = '3214')

此外,我希望命名的查询被用于bcz在结果匹配的查询是必要的
我现在有这个

$params['body']['query']['bool']['must']['bool']['should'][]=[
                'match' => [
                    'stock_code' => [
                        'query'     => $val->stock_code,
                        '_name'      => $val->stock_code,
                    ],
                ]
            ];
yhuiod9q

yhuiod9q1#

您需要使用两个bool/filter(即内部AND)在bool/should(即,外部OR):

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "stock_code": {
                    "value": "1234",
                    "_name": "query1"
                  }
                }
              },
              {
                "term": {
                  "category_code": {
                    "value": "1234",
                    "_name": "query2"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "value": "5678",
                  "_name": "query3"
                }
              },
              {
                "term": {
                  "category_code": {
                    "value": "5678",
                    "_name": "query4"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

相关问题