【问题标题】:Query Match For Integer Number In Elastic Search With Laravel使用 Laravel 进行弹性搜索中整数的查询匹配
【发布时间】:2019-11-19 16:21:37
【问题描述】:

我按照这篇文章连接 laravel 和 ElasticSearch: https://madewithlove.be/how-to-integrate-your-laravel-app-with-elasticsearch/

我在 MySQL 数据库中有一个表,它具有树字段:“title”、“description”和“price”。 “标题”和“描述”是字符串类型。所以我写了下面的代码进行搜索,这工作正常:

private function searchOnElasticsearch(string $query): array
{
    $instance = new Article;

    $items = $this->search->search([
        'index' => $instance->getSearchIndex(),
        'type' => $instance->getSearchType(),
        'body' => [
            'query' => [
                'multi_match' => [
                    'fields' => ['title', 'description'],
                    'query' => $query,
                ],
            ],
        ],
    ]);

    return $items;
}

但是现在,我需要检查 bigInt 类型 的“价格”字段,就像其他两个字段一样:'fields' => ['title', 'description','price'],

更新

当我在字段中添加“价格”时,我在 laravel 中收到此错误:

{"error":{"root_cause":[{"type":"query_shard_exception","re​​ason":"失败 创建查询: {\n \"multi_match\" : {\n \"query\" : \"bad\",\n \"字段\" : [\n \"描述^1.0\",\n \"价格^1.0\",\n \"tags^1.0\",\n \"title^1.0\"\n ],\n \"type\" : \"best_fields\",\n \"operator\" : \"OR\",\n \"slop\" : 0,\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}","index_uuid":"mm787GxMS4WjjjqwRD5YIw","index":"commodities"}],"type":"search_phase_execution_exception","re​​ason":"all 碎片 失败","阶段":"查询","分组":true,"failed_shards":[{"shard":0,"index":"commodities","node":"JYWiIdbLQtu3aWkm_F-e9Q","re​​ason" :{"type":"query_shard_exception","re​​ason":"失败 创建查询: {\n \"multi_match\" : {\n \"query\" : \"bad\",\n \"字段\" : [\n \"描述^1.0\",\n \"价格^1.0\",\n \"tags^1.0\",\n \"title^1.0\"\n ],\n \"type\" : \"best_fields\",\n \"operator\" : \"OR\",\n \"slop\" : 0,\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}","index_uuid":"mm787GxMS4WjjjqwRD5YIw","index":"commodities","caused_by":{"type":"number_format_exception","re​​ason":"For 输入字符串:\"bad\""}}}]},"status":400}

【问题讨论】:

  • 当您将price 字段添加到您的查询时会发生什么?
  • 请将完整的错误添加到您的帖子中,它比 cmets 更清晰

标签: php laravel elasticsearch


【解决方案1】:

我建议这样的事情,即将查询分成两部分,一个用于文本字段,另一个用于数字字段。但是,如果 $query 确实是数字,则仅在数字字段上添加匹配项:

$body = [
    'query' => [
        'bool' => [
            'should' => [
                [
                   'multi_match' => [
                       'fields' => ['title', 'description'],
                       'query' => $query
                   ]
                ]
            ]
        ]
    ]
];

// if the query is numeric add an additional clause for the price field
if (is_numeric($query)) {
    $body['query']['bool']['should'][] = [ 'match' => [ 'price' => $query]];
}

$items = $this->search->search([
    'index' => $instance->getSearchIndex(),
    'type' => $instance->getSearchType(),
    'body' => $body
]);

【讨论】:

  • 感谢您的尝试,我更改了我的代码。但我得到同样的错误......当我评论“匹配”部分程序运行时。这表明错误来自这部分......另外,我将“必须”改为“应该”
  • Doh,你是对的,如果添加一些仅在 $query 是数字时添加价格匹配的逻辑呢?这可能是让它发挥作用的唯一方法。
  • 请向我解释更多,如果你能给我一个代码。我找到了这个解决方案link,但我无法转换为 laravel 集合格式。请查看此链接,如果您可以转换,请发布您的答案。谢谢
  • 非常感谢!你的回答正确。我接受并投票赞成你的回答
  • 太棒了,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 2018-09-08
  • 1970-01-01
  • 2021-11-04
相关资源
最近更新 更多