代码之家  ›  专栏  ›  技术社区  ›  Mickaël Leger

在cloudsearch上查找前缀为awsssdk for php的短语

  •  0
  • Mickaël Leger  · 技术社区  · 6 年前

    在我们的后台,我们将使用cloudsearch来搜索部分,而不是一些mysql请求。

    问题是我在使用CloudSearch获得相同结果时遇到了一些困难,如果可能的话,我希望得到一些帮助…

    例如,如果用户搜索 亚历山德拉呼叫 “:

    使用MySQL :名为“”的事件的一个结果 亚历山大 &布拉布拉布拉(amp;B) 呼叫 “XXX”

    有关更多信息,mysql请求使用 ... WHERE CONCAT(FIELD1, " ", FIELD2, " ", FIELD 3) LIKE '%alexandre%' AND CONCAT(FIELD1, " ", FIELD2, " ", FIELD 3) LIKE '%call%' 以我为例。

    使用CloudSearch :280个结果,其中包含“ 亚历山大 “或包含的事件” 呼叫 “或带后缀的单词” 呼叫 +“一些东西”

    这就是我使用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')
    )
    

    但不可能让它起作用……这个想法应该是 alexandre 在我搜索的3个字段中至少搜索一次 call (对于“呼叫”、“呼叫”、“呼叫”等术语),有什么想法吗?

    编辑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* call* 或者是说我在寻找 亚历山大* 打电话* 在我提到的3个领域?

    正如我之前展示的,我想搜索 亚历山大* 在我提到的三个领域之一, 调用* 在我提到的3个字段中,至少有一个字段

    2 回复  |  直到 6 年前
        1
  •  1
  •   dmbaughman    6 年前

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

    (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 (或者取消这个选项,因为 是默认值)。

    希望有帮助!

    参考文献: AWS CloudSearch Documentation - Searching for Prefixes

        2
  •  0
  •   Mickaël Leger    6 年前

    我接受另一个解决方案,但如果人们希望看到它的外观,我仍然会添加我真正做的事情 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/现在我只需要 用于PHP的AWS SDK 要得到我的结果:

    $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 但这样我就达到了我想要的!