简体   繁体   English

如何对部分Elasticsearch查询进行全文搜索匹配

[英]How to do full text search matching on partial Elasticsearch queries

I use Elasticsearch with Laravel, I implement everything and it works with simple match queries like in the code above. 我将Elasticsearch与Laravel结合使用,实现了所有功能,并且可以与简单的匹配查询一起使用,如上面的代码中所示。

'body' => [
                "query" => [
                       "bool" => [
                           "should" => [
                            ["regexp" => [
                               "tags" => [
                                   "value" => ".{2,8}" . $query . ".*",
                            ]
                            ],
                                ],
                            ["wildcard" => [
                               "tags" => [
                                   "value" => "*" . $query . "*",
                                   "boost" => 1.0,
                                   "rewrite" => "constant_score"
                                    ]
                                ]
                            ]
                        ]],
                    ], "highlight" => [
                    "fields" => [
                        "tags" => ["type" => "plain"]
                    ]
                ]
          ]

I receive good results like on query "java" I receive both "javascript" & "nativjavascript" But the problem is with receiving results in phrases. 我收到了类似查询“ java”的良好结果,同时收到了“ javascript”和“ nativjavascript”,但是问题在于接收短语结果。

I want to type in query "java react" and want to receive this results: "java","javascript","javascript reactjs", "reactjs","react", "nativjavascript". 我想输入查询“ java react”,并希望接收以下结果:“ java”,“ javascript”,“ javascript reactjs”,“ reactjs”,“ react”,“ nativjavascript”。

I am not familiar with Laravel, hence can't provide you the exact syntax but can tell the approach and how I solved this use-case in my application. 我对Laravel不熟悉,因此无法为您提供确切的语法,但可以告诉您该方法以及我如何在应用程序中解决此用例。

  1. Create an n-gram based analyzer on your searchable fields, which would split tokens based on the n-gram you configure. 在可搜索字段上创建一个基于n-gram的分析器,该分析器将根据您配置的n-gram拆分标记。
  2. Split your search term based on the space , in your case java react , should be split into 2 search term java and react . 根据space拆分搜索词,在您使用java react的情况下,应将其拆分为2个搜索词java and react
  3. Use keyword analyzer as a search_time analyzer on the search fields. 在搜索字段上将关键字分析器用作search_time分析器。

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

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