重新翻译为:"PHP语法的ElasticSearch"
P粉176203781
P粉176203781 2023-08-28 15:43:18
[PHP讨论组]
<p>我是ElasticSearch的新手,对查询的工作原理不太了解... 我的索引示例</p> <pre class="brush:php;toolbar:false;">{ &quot;_index&quot; : &quot;indexing001&quot;, &quot;_type&quot; : &quot;_doc&quot;, &quot;_id&quot; : &quot;3&quot;, &quot;_version&quot; : 1, &quot;_seq_no&quot; : 242, &quot;_primary_term&quot; : 2, &quot;found&quot; : true, &quot;_source&quot; : { &quot;type&quot; : 1, &quot;sub_type&quot; : null, &quot;user&quot; : { &quot;id&quot; : 1, &quot;name&quot; : &quot;tk6z2 gcnouvqmr&quot; }, &quot;editor_user&quot; : [ ], &quot;content&quot; : [ { &quot;title&quot; : &quot;Title #3&quot;, &quot;short_text&quot; : &quot;Article #3 short text&quot;, &quot;full_text&quot; : &quot;Article #3 full text&quot;, &quot;locale&quot; : &quot;de-DE&quot; } ], &quot;flags&quot; : [ ], &quot;location&quot; : [ ], &quot;start_date&quot; : 1658793600, &quot;end_date&quot; : 1658793600, &quot;_users&quot; : [ ] } }</pre> <p>我想查询文本以匹配字段<code>content.title</code>和<code>content.short_text</code>,通过<code>_users</code>字段查询用户。 例如,我的函数是:</p> <pre class="brush:php;toolbar:false;">public static function search( string $text = '', int $user = 0 ): array { try { $model = new someModelClass(); $fields = [ 'content.title', 'content.short_text', ]; $result = $model::find()-&gt;query( [ 'bool' =&gt; [ 'should' =&gt; [ 'multi_match' =&gt; [ 'query' =&gt; $text, 'fields' =&gt; $fields, ], ], 'filter' =&gt; [ [ 'term' =&gt; [ '_users.id' =&gt; $user ] ], [ 'term' =&gt; [ '_users' =&gt; [] ] ], ] ], ] )-&gt;all(); return $result; } catch ( Exception $e ) { throw new Exception( $e-&gt;getMessage() ); } }</pre> <p>将其转换为SQL应该是这样的:<code>SELECT * FROM 'indexing001' WHERE (content.title LIKE %search% OR content.short_text LIKE %search%) AND (users.id = 1 OR users = '')</code> 如何在ElasticSearch查询中编写它? 提前感谢!</p>
P粉176203781
P粉176203781

全部回复(1)
P粉421119778

在这种情况下,我建议使用Elasticsearch-PHP客户端。

请使用composer 安装适当的客户端。

对于如下的匹配查询

curl -XGET 'localhost:9200/my_index/_search' -d '{
    "query" : {
        "match" : {
            "testField" : "abc"
        }
    }
}'

您可以在您的PHP脚本中进行查询,如下所示

$params = [
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$results = $client->search($params);

这里查看更多操作。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号