Elasticsearch应该在多个字段上运算

时间:2017-09-22 13:24:03

标签: elasticsearch

目前,我正在尝试使用should子句在多个字段中查询elasticsearch,并在一个字段中查询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']
              ]

        ]
        ]
       ]
    ]
];

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

有没有人有解决方案?

2 个答案:

答案 0 :(得分:1)

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

$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']
                    ]
                  ]
                ]
            ]
        ]
    ];

答案 1 :(得分:0)

您可以像这样编写查询。在您添加Must

的内部添加Should
{
    "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
}