Elasticsearch搜索结果过滤

时间:2018-08-15 11:06:23

标签: php elasticsearch

我是Elasticsearch的新手,并且正在使用REST API for PHP来处理返回的数据。我正在使用以下代码来检索数据。

$params = [
        'index' => 'my_search',
        'type' => 'mytype',
        'from' => 0,
        'size' => 10,
        'body' => [
            'query' => [
                'bool' => [
                    'must' => [
                        [ 'match' => [ 'validated' => true ] ],
                        [ 'match' => [ 'image' => true ] ]
                    ]
                ]
            ],
            'sort' => [
                'created_at' => [ 'order' => 'asc']
            ]
        ]
    ];

以上代码返回的数据完全匹配“ validated => true”和“ image => true”。

此外,我想添加开放文本搜索,就像我们使用/ _search /?q = Apple macbook一样。我尝试使用match,multi_match,query_string选项,但无法成功。

因此,我想从ES中检索具有“ validated => true”,“ image => true”并与文本“ Apple macbook”匹配的结果。

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用query_stringsimple_query_string

$params = [
    'index' => 'my_search',
    'type' => 'mytype',
    'from' => 0,
    'size' => 10,
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    [ 'match' => [ 'validated' => true ] ],
                    [ 'match' => [ 'image' => true ] ],
                    [ 'query_string' => [ 'query' => 'Apple macbook' ] ]
                ]
            ]
        ],
        'sort' => [
            'created_at' => [ 'order' => 'asc']
        ]
    ]
    ];



$params = [
    'index' => 'my_search',
    'type' => 'mytype',
    'from' => 0,
    'size' => 10,
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    [ 'match' => [ 'validated' => true ] ],
                    [ 'match' => [ 'image' => true ] ],
                    [ 'simple_query_string' => [ 'query' => 'Apple macbook' ] ]
                ]
            ]
        ],
        'sort' => [
            'created_at' => [ 'order' => 'asc']
        ]
    ]
    ];

答案 1 :(得分:0)

您也可以通过 为索引启用all_field映射,您可以按照以下网址进行操作  https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html 然后使用以下ES查询:

$params = [
        'index' => 'my_search',
        'type' => 'mytype',
        'from' => 0,
        'size' => 10,
        'body' => [
            'query' => [
                'bool' => [
                    'must' => [
                        [ 'match' => [ '_all' => 'Apple macbook' ] ],
                        [ 'match' => [ 'validated' => true ] ],
                        [ 'match' => [ 'image' => true ] ]
                    ]
                ]
            ],
            'sort' => [
                'created_at' => [ 'order' => 'asc']
            ]
        ]
    ];