【问题标题】:Find phrase with prefix with AWS SDK for PHP on CloudSearch在 CloudSearch 上查找带有 AWS SDK for PHP 前缀的短语
【发布时间】:2018-07-17 13:15:19
【问题描述】:

在我们的后台,我们将使用 Cloudsearch 作为搜索部分,而不是一些 Mysql 请求。

问题是我在使用 Cloudsearch 获得相同结果时遇到了一些麻烦,如果可能的话,我希望得到一些帮助...

例如,如果用户搜索“Alexandre Call”:

使用 Mysql:名为“Alexandre & blablabla CallXXX”的事件的一个结果

有关更多信息,Mysql 请求在我的示例中使用了一些 ... WHERE CONCAT(FIELD1, " ", FIELD2, " ", FIELD 3) LIKE '%alexandre%' AND CONCAT(FIELD1, " ", FIELD2, " ", FIELD 3) LIKE '%call%'

使用 Cloudsearch:280 个结果,其中事件包含“Alexandre”或事件包含“call”或后缀为“打电话 + 某事"

这就是我使用 Cloudsearch 和 AWS SDK for PHP 的方式:

1/ 我连接到我的搜索域:

$client = CloudSearchDomainClient::factory(array(
    'endpoint' => 'https://XXXXXXXX.eu-west-1.cloudsearch.amazonaws.com',
    'validation' => false,
    'version' => 'latest',
    'credentials' => [
        'key'    => _S3_API_KEY_,
        'secret' => _S3_API_SECRET_,
    ],
    'region' =>  'eu-west-1',
));

2/ 我的研究:

$search_result['event'] = $client->search(
    array(
        // This is what user search, in my example $query = "Alexandre Call"
        'query'        => $query, 
        'queryOptions' => '{
                             "defaultOperator" : "or",
                             "fields":["description","nomevent^3", "idftpevent^2"]
                           }',
        'queryParser'  => 'simple',
        'size'         => 500,
        'start'        => 0
    )
);

正如我所说,我得到了 280 个结果...知道如何获得与 mysql 类似的结果吗?

编辑:

我想我想搜索这样的东西,但我不知道如何:

(and 
  (or description='alexandre' nomevent='alexandre' idftpevent='alexandre') 
  (or description='call' nomevent='call' idftpevent='call')
)

但不可能使它工作......这个想法应该是在我搜索的 3 个字段中至少有一次 call (对于像 call、calling、callXXX 等术语),有什么想法吗?

编辑 2

我尝试了我的示例的解决方案:

$event_query = 'alexander* call*';

$search_result['event'] = $client->search(
    array(
        'query'        => $event_query,
        'queryOptions' => '{
                               "defaultOperator": "and",
                               "fields":["nomevent^3","idftpevent^2", "description"]
                           }',
        'queryParser'  => 'simple',
        'size'         => 500,
        'start'        => 0
    )
);

但我没有得到任何结果...我做错了什么?

我很难理解"defaultOperator": "and", 的用途?这意味着我搜索alexandre* AND call* 还是意味着我在我提到的3 个字段中搜索alexandre*call*

如前所述,我想在我提到的 3 个字段之一中搜索 alexandre* 并在我提到的 3 个字段中的至少一个中搜索 call*

【问题讨论】:

  • mysql 调用的 PHP 代码在哪里?
  • 本质上是一个类似的查询。你可能想在空格上爆炸字符串,然后它会是LIKE '%wordone%wordtwo%etc%
  • 我按照你的要求添加了 mysql 信息,但我想通过 Cloudsearch 使用 AWS SDK for PHP 来实现它
  • 哦,我把你的问题弄错了,我认为 aws 返回了正确的东西

标签: php amazon-web-services amazon-cloudsearch


【解决方案1】:

我想我想搜索这样的东西,但我不知道如何:

(and 
  (or description='alexandre' nomevent='alexandre' idftpevent='alexandre') 
  (or description='call' nomevent='call' idftpevent='call')
)

这非常接近,但我认为您可以通过利用来自simple 查询解析器的内置运算符来简化这个很多。使用该解析器,前缀查询在单词或短语的末尾使用 * 来指示您正在搜索以前面字符开头的匹配项。因此,您的查询应如下所示:

/* Only treat the last word as a prefix */

alexandre call*

/* Treat each word as a prefix */

alexandre* call*

现在,要匹配您尝试的复合查询的and 逻辑,您只需将defaultOperator 更改为and(或删除该选项,因为and 是默认值)。

希望有帮助!

参考:AWS CloudSearch Documentation - Searching for Prefixes

【讨论】:

  • 我会试试你的解决方案,我终于设法让我的查询与我想要的 AWS SDK for php 一起工作,但是如果我有callXX 而不仅仅是call,我得到了我的结果。我会尝试你的解决方案,它更接近我想要的:)
  • 我试过了,但没有结果,我应该有一个......我做错了什么吗?
【解决方案2】:

我接受其他解决方案,但如果人们想看看 AWS SDK for PHP 的外观,我仍然会添加我真正所做的:

1/ 我得到用户搜索并“清理”搜索:

$_REQUEST['search'] = preg_replace(
    array('/\+/', '/-/', '/~/', '/>/', '/</', '/"/', '/\'/', '/\)/', '/\(/'), 
    "", 
    $_REQUEST['search']
);

2/ 然后我展开搜索以逐个获取每个单词:

$word_list = explode(" ", trim($_REQUEST['search']));

3/ 现在我可以构建我的请求了:

// I start my query     
$event_query = "(and ";

// Now for each word, I add my condition :
// - I want each searched word in at least one of my 3 fields (nomevent, idftpevent and description)
// - I want the exact word or just a prefix
foreach ($word_list as $word) {
    $event_query .= "(or 
                       (prefix field=description '".$word."')
                       (prefix field=nomevent '".$word."')
                       (prefix field=idftpevent '".$word."')
                       (term field=description '".$word."')
                       (term field=nomevent '".$word."')
                       (term field=idftpevent '".$word."')
                     )";
}

// I close my query
$event_query .= ")";

4/ 现在我只需要使用AWS SDK for PHP 来获得我的结果:

$search_result['event'] = $client->search(
    array(
        'query'        => $event_query,
        'queryParser'  => 'structured',
        'size'         => 500,
        'start'        => 0
    )
);

$search_result['event'] = $search_result['event']->toArray();

结果在$search_result['event']['hits']['hit'],也许有更好的方法来获得那些hit,但这样我就实现了我想要的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多