【问题标题】:How to convert MySQL search criteria to Elasticsearch query如何将 MySQL 搜索条件转换为 Elasticsearch 查询
【发布时间】:2020-01-10 09:55:37
【问题描述】:

我正在尝试将 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);

【问题讨论】:

  • array("wildcard" => array("document_title" => "never Listened", )), array("wildcard" => array("document_content" => "never Listened"))应该是应该的一部分

标签: php elasticsearch


【解决方案1】:

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

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"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2018-09-23
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    相关资源
    最近更新 更多