K
K
Konstantin2015-03-29 02:27:35
PHP
Konstantin, 2015-03-29 02:27:35

How to do Autocomplete search with ElasticSearch?

Who can share an example of how to do an Autocomplete search using ElasticSearch? Do I need to use AJAX to send a GET request to the desired URL? Diagram is not clear at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MintTea, 2015-03-29
@Junart1

I've personally used the Completion Suggester , which is fine if you know in advance what queries the user might be looking for. In my case, these were the names of settlements.
The general scheme is something like this. Depending on the framework you use and the php client for elastic, you can add the code yourself. I'll also omit the details about setting up Russian morphology if you need it, because. this is an even broader issue.
1. Create a separate index for the suggester
2. Create a separate type in this index and give it the correct mapping

curl -XPUT http://localhost:9200/typeahead/locality/_mapping -d '{
    "properties": {
        "suggest": {
            "type": "completion"
        }
    }
}'

3. Fill it with your data
curl -XPOST http://localhost:9200/typeahead/locality/ -d '{
    "suggest": {
        "output": "Москва, Московская область, Россия",
        "input": ["Москва"]
    }
}'
curl -XPOST http://localhost:9200/typeahead/locality/ -d '{
    "suggest": {
        "output": "Новосибирск, Новосибирская область, Россия",
        "input": ["Новосибирск"]
    }
}'

4. Create a controller in your application that will respond to ajax requests
5. Inside the controller, pull the elasticsearch server
curl -XGET http://localhost:9200/typeahead/_suggest -d '{
    "locality-suggest" : {
        "text" : "Новосиб",
        "completion" : {
            "field" : "suggest"
        }
    }
}'

6. On the client, hang up a handler on the input letter event and pull your controller with ajax
7. Render the received response as an input proposal
8. Profit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question