4
votes

I need to prioritize search results that start with the search term. I used "match_phrase_prefix". I used "match_phrase_prefix". But isn't working.

Details:

  • Query

         GET /test/products/_search?pretty
            { 
              "query": {
                "match_phrase_prefix": {
                 "title": "iphone 5c"
                }
              }
            }
    
  • Result

        "_score": 10.440153
        title": "Capa de Tpu para Iphone 5c Modelo Apple + Película"
    
    
    "_score": 9.981493,
    "title": "Capa Bumper Iphone 5C + Pelicula",
    
    "_score": 8.610232
    "title": "Pelicula Protetora para Iphone 5C Transparente"
    
    "_score": 5.154923,
    "title": "iPhone 5c Apple 8GB com Tela de 4”, iOS7, Câmera 8MP, Touch Screen, Wi-Fi, 3G/4G, GPS, MP3 e Bluetooth - Rosa"
    
    "_score": 5.154923
    "title": "iPhone 5c Apple 8GB com Tela de 4”, iOS7, Câmera 8MP, Touch Screen, Wi-Fi, 3G/4G, GPS, MP3 e Bluetooth - Branco"
    
    "_score": 5.154923
    "title": "iPhone 5c Apple 8GB com Tela de 4”, iOS7, Câmera 8MP, Touch Screen, Wi-Fi, 3G/4G, GPS, MP3 e Bluetooth - Preto"
    
  • Mappings

        "mappings": {
            "products": {
              "properties": {
                "title": {
                   "type": "string",
                   "analyzer": "brazilian"
                }
              }
            }
        }
    



In this example I need the first result is the device (Iphone 5c)


1

1 Answers

2
votes

It's the use of analyser which is causing tokenization problem. Have a look below how your document field is getting tokenized.

You need to use keyword tokenizer.

curl -XGET 'localhost:9200/test/_analyze?analyzer=standard&pretty' -d 'Capa Bumper Iphone 5C' | grep token
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   532  100   511  100    21  32800   1347 --:--:-- --:--:-- --:--:-- 34066
  "tokens" : [ {
    "token" : "capa",
    "token" : "bumper",
    "token" : "iphone",
    "token" : "5c",

Add a keyword tokenizer in your mapping:

https://www.elastic.co/blog/starts-with-phrase-matching