0

I’m trying to use Elasticsearch to index a city database and get autocomplete on a field.

Here is my FOSElasticaBundle configuration :

fos_elastica:
    indexes:
        xxxxxxx:
            settings:
                index:
                    analysis:
                        analyzer:
                            custom_analyzer:
                                type:      custom
                                tokenizer: nGram
                                filter:    [lowercase, asciifolding, stopwords_fr, elision, snowball_fr, word_delimiter]
                            custom_search_analyzer:
                                type:      custom
                                tokenizer: standard
                                filter:    [lowercase, asciifolding, stopwords_fr, elision, snowball_fr, word_delimiter]
                        tokenizer:
                            nGram:
                              type:     nGram
                              min_gram: 4
                              max_gram: 20
                        filter:
                            snowball_fr:
                                type:     snowball
                                language: French
                            elision:
                                type:     elision
                                articles: [l, m, t, qu, n, s, j, d]
                            stopwords_fr:
                                type:        stop
                                stopwords:   [_french_]
                                ignore_case: true
            types:
                cities:
                    mappings:
                        id:
                        name: { search_analyzer: custom_analyzer, index_analyzer: custom_analyzer, type: string, store: yes }
                persistence:
                    driver:     orm
                    model:      XXXXX\MainBundle\Entity\City
                    provider:   { query_builder_method: createSearchIndexQueryBuilder }
                    listener:   ~
                    finder:     ~
                    repository: XXXXX\SearchBundle\Repository\CitySearchRepository

My query looks like this :

{
  "query": {
    "match": {
      "name": "xxxxxx"
    }
  }
}

But my problem is here :

  • When I type “Pa”, I get 242 results (ok)
  • When I type “Par”, I get no results (WTF)
  • When I type “Pari”, I get 22 results (ok)
  • When I type “Paris”, I get 10 results (ok)

It’s my first time with Elasticsearch, I think the solution is nearby, but if somebody has the same issue, I’m curious to know more about it.

Thanks, have a good day ;)

Sylvain
  • 2,742
  • 5
  • 21
  • 34

1 Answers1

0

In your query you are doing a match. It means that you will look for documents that have the (analyzed) value you are looking for.

You should have a look on the completion suggester, it is the best way to implement suggestions for autocomplete.

If you are lazy you can just use a prefix query:

{
    "prefix" : { "user" : "Pa" }
}
Heschoon
  • 2,915
  • 9
  • 26
  • 55