简体   繁体   中英

Querying Elasticsearch with PHP

I'm having a little trouble translating some of the queries I use for elasticsearch into PHP readable queries.

For example this simple query works:

$query = $elastic->search([
body' => [
    'query' => [
        'match' => [
            'myfield' => 'mymatchingresult'
        ]
    ]
]
]);

But what I'm trying to get to work follow below. There isn't an error, it just doesn't run. I must not be understanding the structure. The same query if placed in something like google extension sense seems to work. (With the php '=>' converted to ':' etc.)

$query = $elastic->search([
'body' => [
    'query' => [
        'filtered' => [
            'query' => [
                'query_string' => [
                    'query' => '*',
                    'analyze_wildcard' => 'true'
                ]
            ],
            'filter' => [
                'bool' => [
                    'must' => [ 
                        'query' => [
                            'query_string' => [
                                'analyze_wildcard' => 'true',
                                'query' => 'cn:name'
                            ]
                        ],
                        'range' => [
                            '@timestamp' => [
                                'from' => '2012-05-01',
                                'to' => '2016-05-01'
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]
]);

Thank you for the help! -John

As far as I can tell, the constraints in your bool/must filter must be enclosed in square brackets, ie bool/must should be a pure array, not an associative array.

Like this:

$query = $elastic->search([
'body' => [
    'query' => [
        'filtered' => [
            'query' => [
                'query_string' => [
                    'query' => '*',
                    'analyze_wildcard' => 'true'
                ]
            ],
            'filter' => [
                'bool' => [
                    'must' => [ 
                      [
                        'query' => [
                            'query_string' => [
                                'analyze_wildcard' => 'true',
                                'query' => 'cn:name'
                            ]
                        ]
                      ],
                      [
                        'range' => [
                            '@timestamp' => [
                                'from' => '2012-05-01',
                                'to' => '2016-05-01'
                            ]
                        ]
                      ]
                    ]
                ]
            ]
        ]
    ]
]
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM