PHP 中用于 PostgreSQL 的布尔搜索处理

发布于 2024-11-18 00:49:05 字数 360 浏览 2 评论 0原文

这似乎是一个显而易见的问题,但目前似乎没有任何答案。

我希望尝试处理搜索查询,以使用 PHP 进行正确格式的 postgresql 全文搜索。我是正则表达式的新手,我似乎不知道从哪里开始。

我希望按照 Something AND ("Some Phrase" OR Some Other Phrase) 的方式进行查询,并将其转换为 'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))

您也许可以向我指出一个可以执行此操作的库,但我不能似乎找到了一个,尽管我认为这是一个常见问题。感谢您的帮助!

This seems like an obvious question but there does not appear to be an answer anywhere at the moment.

I am looking to attempt to process a search query to go into a postgresql full-text search with correct formatting using PHP. I am a bit of newbie to regular expressions and I just can't seem to work out where to start.

I am looking to take a query along the lines ofSomething AND ("Some Phrase" OR Some Other Phrase) and convert it to 'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))

You may be able to point me to a library that does this, I can't seem to find one although I imagine it is a common problem. Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-11-25 00:49:05

此代码将解析给定的示例并返回请求的输出。请注意,它有点脆弱:您可能需要验证表达式的格式以确保它与您发布的格式相似。

$input = 'Something AND ("Some Phrase" OR Some Other Phrase)';

$formatted_output = format_input( $input );

// Convert a query to a format suitable for a Postgres full-text search
function format_input( $input ) {
    $output = '';
    list ( $part1, $part2 ) = explode( 'AND', $input );

    // Remove any unecessary characters and add the first part of the line format
    $output = "'" . str_replace( array( "\"", "'" ), '', trim( $part1 ) ) . "' & (";

    // Get a list of phrases in the query
    $phrases = explode( 'OR', str_replace( array( '(', ')'), '', $part2 ) );

    // Format the phrase
    foreach ( $phrases as &$phrase ) {
        $phrase = encapsulate_phrase( trim ( str_replace( array( "\"", "'"), '', $phrase ) ) );
    }

    // Add the formatted phrases to the output
    $output .= '(' . implode( ')|(', $phrases ) . ')';

    // Add the closing parenthesis
    $output .= ')';

    return $output;
}

// Split a search phrase into words, and encapsulate the words
function encapsulate_phrase( $phrase ) {
    $output = '';
    $words = explode( ' ', trim( $phrase ) );

    // Remove leading and trailing whitespace, and encapsulate words in single quotes
    foreach ( $words as &$word ) {
        $word = "'" . trim( $word ) . "'";
    }

    // Add each word to the output
    $output .= implode (" & ", $words);


    return $output;
}

您可以像这样测试您的输入:

$desired_output = "'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))";

if ( !assert ( $formatted_output == $desired_output ) ) {
    echo "Desired: $desired_output\n";
    echo "Actual:  $formatted_output\n";
}
else {
    echo "Output: $formatted_output\n";
}

This code will parse the example as given and return the requested output. Be aware that it is somewhat brittle: You may need to validate the format of the expression to ensure that it is similar to the format you posted.

$input = 'Something AND ("Some Phrase" OR Some Other Phrase)';

$formatted_output = format_input( $input );

// Convert a query to a format suitable for a Postgres full-text search
function format_input( $input ) {
    $output = '';
    list ( $part1, $part2 ) = explode( 'AND', $input );

    // Remove any unecessary characters and add the first part of the line format
    $output = "'" . str_replace( array( "\"", "'" ), '', trim( $part1 ) ) . "' & (";

    // Get a list of phrases in the query
    $phrases = explode( 'OR', str_replace( array( '(', ')'), '', $part2 ) );

    // Format the phrase
    foreach ( $phrases as &$phrase ) {
        $phrase = encapsulate_phrase( trim ( str_replace( array( "\"", "'"), '', $phrase ) ) );
    }

    // Add the formatted phrases to the output
    $output .= '(' . implode( ')|(', $phrases ) . ')';

    // Add the closing parenthesis
    $output .= ')';

    return $output;
}

// Split a search phrase into words, and encapsulate the words
function encapsulate_phrase( $phrase ) {
    $output = '';
    $words = explode( ' ', trim( $phrase ) );

    // Remove leading and trailing whitespace, and encapsulate words in single quotes
    foreach ( $words as &$word ) {
        $word = "'" . trim( $word ) . "'";
    }

    // Add each word to the output
    $output .= implode (" & ", $words);


    return $output;
}

You can test your inputs like this:

$desired_output = "'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))";

if ( !assert ( $formatted_output == $desired_output ) ) {
    echo "Desired: $desired_output\n";
    echo "Actual:  $formatted_output\n";
}
else {
    echo "Output: $formatted_output\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文