【问题标题】:how to find a string within a string in elastic search via elastica?如何通过elastica在弹性搜索中的字符串中查找字符串?
【发布时间】:2025-12-16 03:55:02
【问题描述】:

假设我想要一个搜索结果,其中每个项目的这个特定字段都有字符串,例如 # 标签

例如,我有一个名为 description 的数据字段,所以我想从结果中得到什么,只有那些在其描述中具有字符串“hashtag”的项目,例如,例如

"the english alphabet has 28 letters #hashtag"

所以,上面有这个描述字段的那个项目应该包含在搜索结果中,因为描述字段值字符串里面有一个“#hashtag”字符串。

如何在 elastica 中做到这一点?我应该使用什么过滤或功能?

【问题讨论】:

    标签: php elasticsearch elastica


    【解决方案1】:
    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);
    
    // The !== operator can also be used.  Using != would not work as expected
    // because the position of 'a' is 0. The statement (0 != false) evaluates 
    // to false.
    if ($pos !== false) {
         echo "The string '$findme' was found in the string '$mystring'";
             echo " and exists at position $pos";
    } else {
         echo "The string '$findme' was not found in the string '$mystring'";
    }
    ?>
    

    【讨论】: