Search code examples
elasticsearchelasticsearch-dsl

Elasticsearch: How to use wildcards in percolator query


I am creating some percolator queries that look like the one below:

{
    "search_term": "*a1c*",
    "highlight": {
        "fields": [
            "assessment",
            "complaint",
            "exam",
            "history"
         ],
        "post_tags": [
            "~~"
        ],
        "pre_tags": [
            "~~"
        ]
    },
    "query": {
        "multi_match": {
            "fields": [
                "assessment",
                "complaint",
                "exam",
                "history",
            ],
            "query": "*a1c*",
            "type": "phrase"
        }
    }
}

What I want is to search a document like

"patient's hemoglobin a1c is ..."

and return the result

"patient's hemoglobin ~~a1c~~ is..."

This is currently working with the query above. However, I want it to also be able to locate the term 'a1c' when it's a substring, like in

"patient's hba1c is..."

or

 "patientsa1cmeasurementis..."

However, elasticsearch only matches 'a1c' as a standalone string, and won't identify substrings despite having the wildcards in the search terms. What am I doing wrong here? If it helps, I don't care about execution time (I know the wildcards are slow) and it would be great to keep the highlighting, but if the two are mutually exclusive then I am okay to get rid of it.


Solution

  • Try changing your multi_search to

    "query":{
      "query_string":{
         "query":"*a1c*",
         "fields":[
            "assessment",
            "complaint",
            "exam"
         ]
      }}
    

    With this query 'patient's hba1c is...' will be 'patient's ~~hba1c~~ is...' and "patientsa1cmeasurementis..." will be "~patientsa1cmeasurementis...~~"