简体   繁体   English

PHP MySQL ElasticSearch 迁移查询 OR

[英]PHP MySQL ElasticSearch migration query OR

I cannot figure out the following:我无法弄清楚以下内容:

A database with address fields (country, county, city) and an "operate" field, options: locally, nationally internationally具有地址字段(国家、县、市)和“操作”字段的数据库,选项:本地、国内、国际

I want to find matches based on location and the "operate" status.我想根据位置和“操作”状态查找匹配项。 So I am basically searching for how to use MySQL OR in ElasticSearch.所以我基本上是在搜索如何在 ElasticSearch 中使用 MySQL 或。 To my understanding this is "should".据我了解,这是“应该”。

So, any entry that operates internationally should be in the results, any entry that operates nationally in the same country and any entry that operates locally in the same county regardless which city is selected因此,任何在国际上运营的条目都应该在结果中,在同一国家/地区在全国范围内运营的任何条目以及在同一县本地运营的任何条目,无论选择哪个城市

Just as a test case, I tried several things among which the following:作为测试用例,我尝试了几件事,其中包括:

$arrayinternationally[] = array('match' => array('operate' => 'internationally'));

$arrayNationally[] = array('match' => array('operate' => 'nationally'));
$arrayNationally[] = array('match' => array('country' => '60'));

$arrayLocally[] = array('match' => array('operate' => 'locally'));
$arrayLocally[] = array('match' => array('country' => 'UK'));
$arrayLocally[] = array('match' => array('county' => '60'));

$params = [
    'index' => 'bzlistings',

    'body' => [
        'from' => 0,
        'size' => 80, 
        'query' => [
            'bool' => [
                'should' => [
                    'bool' => [
                        'should' => $arrayinternationally
                    ]
                ],
                'should' => [
                    'bool' => [
                        'should' => $arrayNationally
                    ]
                ],
                'should' => [
                    'bool' => [
                        'should' => $arrayLocally
                    ]
                ],
            ],
        ],
    ],
];

Those entries in another country with "operate" set to "internationally", are not included, which is wrong.那些在另一个国家将“运营”设置为“国际”的条目不包括在内,这是错误的。

How can this be done in ElasticSearch?这如何在 ElasticSearch 中完成?

Thanks, Peter谢谢,彼得

I think there is something wrong with the bool queries.我认为 bool 查询有问题。 Could you test with following query:您可以使用以下查询进行测试吗:

$params = [
    'index' => 'bzlistings',
    'body' => [
        'from' => 0,
        'size' => 80, 
        'query' => [
            'bool' => [
                'should' => [
                    [
                        'bool' => [
                            'must' => $arrayinternationally
                        ]
                    ],
                    [
                        'bool' => [
                            'must' => $arrayNationally
                        ]
                    ],
                    [
                        'bool' => [
                            'must' => $arrayLocally
                        ]
                    ],
                ],
            ],
        ],
    ],
];

But I think, in your case, inner bools need to be must instead of should .但我认为,在你的情况下,内部布尔值必须是must而不是should Also, I recommend that if you are looking for exact matching, you can use term query instead of match .另外,我建议如果你正在寻找精确匹配,你可以使用term query 而不是match

Typically, the bool query structure is something like the below:通常,bool 查询结构如下所示:

GET sample-index/_search
{
  "query": {
    "bool": {
      "should": [
        {},
        {}
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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