Fixing query for type ahead with more than one query term

Hi,

I'm currently using the following query to suggest terms for type ahead completion for a title field, and it suits me very well:

GET transcricoes/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "pr*"
      }
    }
  },
  "highlight": {
    "fields": {
      "name": {"type": "plain"}},
    "pre_tags": "<hl>",
    "post_tags": "</hl>",
    "fragment_size" : 255,
    "order": "score",
    "fragmenter": "simple",
    "boundary_chars": ".,!?",
    "boundary_scanner": "word"
  },
  "_source": false
}

This query will return results like "Prime" and "Prime Minister", which is correct for my use case.
Problem is when try to add a second term for completion. For example "value": "prime m*". In this case, no results are returned. Any ideas how to fix my query?

Thanks in advance.

Hi @RodAndTom

You haven't provided many details, but the reason it doesn't work is because the token generated isn't composed of prime and minister.
In this case, I suggest using a shingle analyzer to obtain a token with prime and minister so that your query will work.

PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "shingle_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "my_shingle_filter"
          ]
        }
      },
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 3,
          "output_unigrams": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "shingle": {
            "analyzer": "shingle_analyzer",
            "type": "text"
          }
        }
      }
    }
  }
}


POST idx_test/_doc
{
  "name": "prime"
}

POST idx_test/_doc
{
  "name": "prime minister"
}

GET idx_test/_search
{
  "query": {
    "wildcard": {
      "name.shingle": {
        "value": "prime m*"
      }
    }
  }
}

Perhaps take a look at

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.