首页 > 解决方案 > Elasticsearch-php 查询

问题描述

有谁知道弹性搜索-php 示例的好资源,理想情况下涵盖了使用 MySQL 示例的查询。我正在为代码语法和何时使用什么而苦苦挣扎。

例如,我想做一个搜索,其中 $name 必须是字段 'business' 的一部分,并且 'country' 匹配 $country

$params = [
    'index' => 'xxxxx',
    'type' => 'zzzzz',
    'body' => [
        'from' => 0, 
        'size' => $maxResults, 
        'query' => [
            'bool' => [
                'must' => [
                    'match' => ['name' => $searchString],
                ],
                'must' => [
                    'match' => ['country' => $country],
                ],

            ],
        ],
    ],
];

第一个“必须”似乎被完全忽略了。删除它将返回完全相同的结果。

我搜索了几个小时。有很多带有简单搜索示例的快速初学者教程,但我已经像上面的示例一样被卡住了一步

谢谢

标签: phpelasticsearch

解决方案


must一个查询中只能有一个bool,那么所有的约束都必须是must数组的元素。试试这样:

$params = [
    'index' => 'xxxxx',
    'type' => 'zzzzz',
    'body' => [
        'from' => 0, 
        'size' => $maxResults, 
        'query' => [
            'bool' => [
                'must' => [
                  [
                    'match' => ['name' => $searchString],
                  ],
                  [
                    'match' => ['country' => $country],
                  ],
                ]
            ],
        ],
    ],
];

推荐阅读