gpt4 book ai didi

php - 如何将 MySQL 搜索条件转换为 Elasticsearch 查询

转载 作者:搜寻专家 更新时间:2023-10-31 21:19:25 25 4
gpt4 key购买 nike

我正在尝试将 MySQL 搜索条件转换为 Eleasticsearch(版本 7)查询,但是,这两个查询(MySQL 和 Elasticsearch)给出了不同的结果。在 SQL 语句上,它显示单条记录,而在 Elasticsearch 上,它显示 0 条记录。非常感谢任何帮助或指导。

SQL 条件


COUNT(*) WHERE (document_title like '%never listened%' or document_content like '%never listened%') and documnent_tone = 'negative'

转换为 Elasticsearch - 使用 PHP Elasticsearch 库

use Elasticsearch\ClientBuilder;
...

$elasticServer = $this->getOption('elasticsearch')->getElasticServer1();
$hosts = array(
$elasticServer['host'] . ':' . $elasticServer['port']
);
$client = ClientBuilder::create()
->setHosts($hosts)
->build();

$params = array (
"index" => "index_politics",
"body" => array(
"query" => array(
"bool" => array(
"must" => array(
array(
"wildcard" => array(
"document_title" => "never listened",
)
),
array(
"wildcard" => array(
"document_content" => "never listened"
)
),
array(
"match" => array(
"document_tone" => "negative"
)
)
)
)
)
)
);

$response = $client->count($params);
$negative = $response['count'];

var_dump($negative);

最佳答案

经过大量挖掘,以下内容对我有用

use Elasticsearch\ClientBuilder;
...

$elasticServer = $this->getOption('elasticsearch')->getElasticServer1();
$hosts = array(
$elasticServer['host'] . ':' . $elasticServer['port']
);
$client = ClientBuilder::create()
->setHosts($hosts)
->build();

$params = array (
"index" => "index_politics",
"body" => array(
"query" => array(
"bool" => array(
"must" => array(
array(
"multi_match" => array(
"query" => "never listened",
"fields" => array("document_title", "document_content")
)
)
),
"filter" => array(
"term" => array(
"document_tone" => "negative"
)
)
)
)
)
);

$response = $client->count($params);
$negative = $response['count'];

var_dump($negative);

转换为 Elasticsearch 查询

GET /index_politics/_count
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "never listened",
"fields": [
"document_title",
"document_content"
]
}
}
],
"filter": {
"term": {
"document_tone": "negative"
}
}
}
}
}

关于php - 如何将 MySQL 搜索条件转换为 Elasticsearch 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com