当用户类型时

发布于 2025-02-07 03:42:00 字数 1029 浏览 0 评论 0原文

我想索引数据,因此我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2025-02-14 03:42:00

我认为在您的查询中,您需要在弹性搜索API中使用一个或条件,其中录制与字段1或字段2匹配。

您需要在布尔/应该查询中包含两个匹配查询,例如这样

解决您的问题,您需要更改您的问题按照下面的搜索查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "t"
          }
        },
        {
          "match": {
            "search_name": "t"
          }
        }
      ]
    }
  }
}

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 :

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "t"
          }
        },
        {
          "match": {
            "search_name": "t"
          }
        }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文