简体   繁体   English

使用Laravel在弹性搜索中查询整数的匹配项

[英]Query Match For Integer Number In Elastic Search With Laravel

I followed this article for connecting laravel and ElasticSearch: https://madewithlove.be/how-to-integrate-your-laravel-app-with-elasticsearch/ 我跟随这篇文章来连接laravel和ElasticSearch: https ://madewithlove.be/how-to-integrate-your-laravel-app-with-elasticsearch/

I have a table in MySQL database that it has tree field:"title","description" and "price". 我在MySQL数据库中有一个表,该表具有树字段:“标题”,“描述”和“价格”。 "title" and "description is string type. so I write below code for searching and this worked correctly: “ title”和“ description是字符串类型。因此,我在下面编写了用于搜索的代码,此代码可以正常运行:

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

But now, I need to check "price" field that is a bigInt type like two other field: 'fields' => ['title', 'description','price'], 但是现在,我需要检查与其他两个字段一样为bigInt类型的 “价格”字段: 'fields' => ['title', 'description','price'],

UPDATE UPDATE

When I add "price" to field I get this error in laravel: 当我在字段中添加“价格”时,在laravel中出现此错误:

{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\\n \\"multi_match\\" : {\\n \\"query\\" : \\"bad\\",\\n \\"fields\\" : [\\n \\"description^1.0\\",\\n \\"price^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","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"commodities","node":"JYWiIdbLQtu3aWkm_F-e9Q","reason":{"type":"query_shard_exception","reason":"failed to create query: {\\n \\"multi_match\\" : {\\n \\"query\\" : \\"bad\\",\\n \\"fields\\" : [\\n \\"description^1.0\\",\\n \\"price^1.0\\",\\n \\"tags^1.0\\",\\n \\"title^1.0\\"\\n ],\\n \\"type\\" : \\"best_fields\\",\\n \\"oper {“ error”:{“ root_cause”:[{“ type”:“ query_shard_exception”,“ reason”:“无法创建查询:{\\ n \\” multi_match \\“:{\\ n \\” query \\“:\\” bad \\“,\\ n \\” fields \\“:[\\ n \\”描述^ 1.0 \\“,\\ n \\”价格^ 1.0 \\“,\\ n \\”标签^ 1.0 \\“,\\ n \\”标题^ 1.0 \\“ \\ n],\\ n \\”类型\\“:\\”最佳域\\“,\\ n \\”操作符\\“:\\” OR \\“,\\ n \\”倾斜\\“:0,\\ n \\”前缀长度\\ “: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“:”商品“}],”类型“:” search_phase_execution_exception“,”原因“:”所有分片失败“ “相”: “查询”, “分组”:真 “failed_shards”:[{ “碎片”:0, “指数”: “商品”, “节点”: “JYWiIdbLQtu3aWkm_F-E9Q”, “理由”:{ “ type”:“ query_shard_exception”,“ reason”:“无法创建查询:{\\ n \\” multi_match \\“:{\\ n \\” query \\“:\\”坏\\“,\\ n \\”字段\\“: [\\ n \\“说明^ 1.0 \\”,\\ n \\“价格^ 1.0 \\”,\\ n \\“标签^ 1.0 \\”,\\ n \\“标题^ 1.0 \\” \\ n],\\ n \\“类型\\ “:\\” best_fields \\“,\\ n \\” oper ator\\" : \\"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","reason":"For input string: \\"bad\\""}}}]},"status":400} ator \\“:\\” 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“:”商品“,” caused_by“:{” type“:” number_format_exception“,” reason“:”对于输入字符串:\\“ bad \\”“}}}]},” status“:400}

I suggest something like this, ie split the query into two parts, one for text fields and another for numeric fields. 我建议使用类似的方法,即将查询分为两部分,一部分用于文本字段,另一部分用于数字字段。 However, only add the match on numeric fields if the $query is indeed a number: 但是,仅在$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
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM