Imediasun
Imediasun

Reputation: 81

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.

'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.

I want to type in query "java react" and want to receive this results: "java","javascript","javascript reactjs", "reactjs","react", "nativjavascript".

Upvotes: 0

Views: 271

Answers (1)

Amit
Amit

Reputation: 32386

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.

  1. Create an n-gram based analyzer on your searchable fields, which would split tokens based on the n-gram you configure.
  2. Split your search term based on the space, in your case java react, should be split into 2 search term java and react.
  3. Use keyword analyzer as a search_time analyzer on the search fields.

Upvotes: 1

Related Questions