Bert Coenen
Bert Coenen

Reputation: 173

How to search in elastic search to return results like this

I'm struggeling to return the correct results as we wanted. I wanted an autocomplete/search as you type solution. and ES provides that, but isn't working as expected. bv If I search on "diabetes" the exact match is displayed on item 50 (of 100) and the first returned example is an entire sentence.

So I was trying to work with boost. My expactation is to returned the exact match at the top example, then the auto complete, then word in the middle of the sentence. But I don't know how to work with this.

My tryouts but not working: enter image description here enter image description here

"diabetes" "diabetes test abcd" "milde diabetes" "milde diabetes militus"

My mapping is:

{
    "mappings": {
        "properties": {
            "Descriptions": {
                "type": "nested",
                "properties": {
                    "Term": {
                        "type": "search_as_you_type"
                    },
                    "Code": {
                        "type": "search_as_you_type",                      
                    },
                    "System": {
                        "type": "keyword",
                        "index": true
                    },
                    "LanguageId": {
                        "type": "keyword",
                        "index": true
                    },
                    "Purpose": {
                        "type": "keyword",
                        "index": true
                    },
                    "Active": {
                        "type": "keyword",
                        "index": true
                    }
                }
            },
            "Mappings": {
                "properties": {
                    "To": {
                        "type": "keyword",
                        "index": true
                    },
                    "ToSys": {
                        "type": "keyword",
                        "index": true
                    }
                }
            }
        }
    }
}

Objects:

"Code": "10008220/A1",
"Descriptions": [
    {
      
        "Term": "milde diabetes",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
       
    },
    {  
        "Term": "fracture du bras",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6e3",
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

object 2

"Code": "10008220/A1",
"Descriptions": [
    {
      
        "Term": "milde diabetes militus",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
       
    },
    {  
        "Term": "fracture du bras",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6e3",
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

object 3

"Code": "10008220/A1",
"Descriptions": [
    {
      
        "Term": "diabetes",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
       
    },
    {  
        "Term": "fracture du bras",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6e3",
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

object 4

"Code": "10008220/A1",
"Descriptions": [
    {
      
        "Term": "diabetes test abcd",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
       
    },
    {  
        "Term": "fracture du bras",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6e3",
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

object 5

"Code": "10008220/A1",
"Descriptions": [
    {
      
        "Term": "this cannot be returned",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
       
    },
    {  
        "Term": "fracture du bras",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6e3",
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

Upvotes: 0

Views: 223

Answers (1)

ilvar
ilvar

Reputation: 5841

Having a few analyzers for the same field should help:

"Term": {
  "type": "search_as_you_type",
  "fields": {
    "keyword": {"type": "keyword"},
    "standard": {"type": "text", "analyzer": "standard"},
    "french": {"type": "text", "analyzer": "french"},
  }
}

Then you query them all:

"query": {
  "bool": {
    "should": [
      {"match": {"Term": {"query": "diabetes", "boost": 1}}},
      {"match_phrase": {"Term": {"query": "diabetes", "boost": 5}}},
      {"match_phrase_prefix": {"Term": {"query": "diabetes", "boost": 5}}},
      {"match": {"Term.keyword": {"query": "diabetes", "boost": 100}}},
      {"match": {"Term": {"query": "diabetes.standard", "boost": 20}}},
      {"match": {"Term": {"query": "diabetes.french", "boost": 30}}},
    ]
  }
}

You'll have to tune boosting values to get good results for different queries (single word, phrase, multi word, prefix, etc)

Upvotes: 1

Related Questions