当用户类型时
我想索引数据,因此我在Laravel中命令。我安装了Elasticsearch/Elasticsearch:'^7.17'包装,用于Laravel中的弹性搜索。我在下面的Laravel中有查询:
public function search(string $query = ''): Collection
{
return Item::query()
->where('search_name', 'like', "%{$query}%")
->orWhere('name', 'like', "%{$query}%")
->get();
}
因此,我想从两个字段搜索,但我在Elasticsearch中没有找到查询。 你能帮我吗?
当我必须从一个字段搜索时,它正在工作。该查询的代码是:
'body' => [
'query' => [
'query_string' => [
"query" => "*".$query."*",
"fields" => ['search_name']
]
],
],
但是我希望当像 t 这样的用户类型时,它应该从 search_name 或 name 列中搜索。
这样的示例数据:
[
{
"id": 1,
"search_name": "TR61QF",
"name": ""
},
{
"id": 2,
"search_name": "204480"
"name": "Mirae Asset TIGER Synth-China A Leverage"
}
]
当用户搜索 t 时,两个结果都会响应。
I want to indexing data so i made command in laravel. I installed elasticsearch/elasticsearch : '^7.17' package for elastic search in laravel. i have query in laravel below:
public function search(string $query = ''): Collection
{
return Item::query()
->where('search_name', 'like', "%{$query}%")
->orWhere('name', 'like', "%{$query}%")
->get();
}
so, i want to search from two fields but i did not find query in elasticsearch for that.
can you please help me?
when i have to search from one field then it is working. code for that query is:
'body' => [
'query' => [
'query_string' => [
"query" => "*".$query."*",
"fields" => ['search_name']
]
],
],
but i want when user type like t then it should search from search_name or name column from Items table sql database.
sample data like this:
[
{
"id": 1,
"search_name": "TR61QF",
"name": ""
},
{
"id": 2,
"search_name": "204480"
"name": "Mirae Asset TIGER Synth-China A Leverage"
}
]
when user search t then both result arrive in response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在您的查询中,您需要在弹性搜索API中使用一个或条件,其中录制与字段1或字段2匹配。
您需要在布尔/应该查询中包含两个匹配查询,例如这样
解决您的问题,您需要更改您的问题按照下面的搜索查询:
I think in your query you need an OR condition in Elastic search API where either record match with field 1 or field 2.
You need two match queries enclosed in a bool/should query, like this
So for solving your problem you need to change the search query as per like below :