【问题标题】:Elasticsearch should operator on multiple fieldsElasticsearch 应该在多个字段上进行操作
【发布时间】:2017-09-22 23:04:38
【问题描述】:

目前,我正在尝试在多个字段中使用 should 子句以及在一个字段中使用 must 子句来查询弹性搜索。

用 SQL 我会写这个查询:

SELECT * FROM test where ( titleName='Business' OR titleName='Bear') AND (status='Draft' OR status='Void') AND creator='bob'

我试过这个:

$params = [
    'index' => myindex,
    'type' => mytype,
    'body' => [
        "from" => 0,
        "size" => 1000,
        'query' => [
            'bool' => [
           'must' => [
            'bool' => [
              'should' => [
                   ['match' => ['titleName' => 'Business']],
                   ['match' => ['titleName' => 'Bear']]
              ]
             ],
             'should' => [
                   ['match' => ['status' => 'Draft']],
                   ['match' => ['status' => 'Void']]
                ]
              'must' => [
                   ['match'] => ['creator' => 'bob']
              ]

        ]
        ]
       ]
    ]
];

上述查询字符串使用单个状态字段或单个标题字段。但它不适用于这两个领域。

有人有解决办法吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要 AND 两个 bool/should 对。这应该有效:

    $params = [
            'index' => myindex,
            'type' => mytype,
            'body' => [
                "from" => 0,
                "size" => 1000,
                'query' => [
                    'bool' => [
                      'must' => [
                        [
                          'bool' => [
                            'should' => [
                               ['match' => ['titleName' => 'Business']],
                               ['match' => ['titleName' => 'Bear']]
                            ]
                          ]
                        ],
                        [
                          'bool' => [
                            'should' => [
                               ['match' => ['status' => 'Draft']],
                               ['match' => ['status' => 'Draft']]
                            ]
                          ]
                        ],
                        [
                           'match' => ['creator' => 'bob']
                        ]
                      ]
                    ]
                ]
            ]
        ];
    

    【讨论】:

      【解决方案2】:

      您可以像这样编写查询。在您添加Should 的内部添加Must

      {
          "query": {
              "filter": {
                  "bool": {
                      "must": [{
                              "bool": {
                                  "should": [{
                                          "term": {
                                              "titleName": "business"
                                          }
                                      },
                                      {
                                          "term": {
                                              "titleName": "bear"
                                          }
                                      }
                                  ]
                              }
                          },
                          {
                              "bool": {
                                  "should": [{
                                          "term": {
                                              "status": "draft"
                                          }
                                      },
                                      {
                                          "term": {
                                              "status": "void"
                                          }
                                      }
                                  ]
                              }
                          },
                          {
                              "bool": {
                                  "must": [{
                                      "term": {
                                          "creator": "bob"
                                      }
                                  }]
                              }
                          }
                      ]
                  }
      
              }
          },
          "from": 0,
          "size": 25
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 2020-07-06
        • 2015-04-13
        • 2017-04-03
        • 2020-10-22
        • 2017-01-17
        相关资源
        最近更新 更多