1 year ago

#76242

test-img

Bert Coenen

ElasticSearch won't search specific field

I have a problem searching a specific field inside my index.

little background:

On my project we need to search inside a terminology Server like FHIR but then our own. So we have an object that contains a Code (123564/A), multiple translations as term/display (urine problem) and mapping to other codes that are equal to that code but in a different system (ICD-10, SNOMED-CT, ICPC-2,..) example what has been indexed:

{
"Code": "10008220/A1",
"EffectiveTime": "0001-01-01T00:00:00Z",
"Active": true,
"System": "ibui",
"Purpose": "",
"Descriptions": [
    {
        "DescriptionId": "2464cf5c-d4fc-4a61-b6bc-746d003cb4ef",
        "Code": "10008220/A1",
        "System": "ibui",
        "Term": "gebroken arm",
        "LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee",
        "FSN": false,
        "Preferred": true,
        "EffectiveTime": "0001-01-01T00:00:00Z",
        "Active": true,
        "SendVersion": "2021-12-07T17:01:53.786755Z",
        "Purpose": ""
    },
    {
        "DescriptionId": "95501583-9f24-4964-bbc9-1a6e95eba30f",
        "Code": "10008220/A1",
        "System": "ibui",
        "Term": "fracture du bras",
        "LanguageId": "1238dde0-08df-4ae0-8676-59919f66737e",
        "FSN": false,
        "Preferred": true,
        "EffectiveTime": "0001-01-01T00:00:00Z",
        "Active": true,
        "SendVersion": "2021-12-07T17:01:53.786755Z",
        "Purpose": ""
    }
],
"Mappings": [
    {
        "MappingId": "",
        "FromSys": "ibui",
        "From": "10008220/A1",
        "ToSys": "icd-10",
        "To": "T10",
        "EffectiveTime": "0001-01-01T00:00:00Z",
        "Active": true
    },
    {
        "MappingId": "",
        "FromSys": "ibui",
        "From": "10008220/A1",
        "ToSys": "icpc-2",
        "To": "L76",
        "EffectiveTime": "0001-01-01T00:00:00Z",
        "Active": true
    }
],
"SendVersion": "2021-12-07T17:01:53.786755Z"

}

The problem: We can search on 2 different fields : Code & Term. and when searching we keep in mind that we have some filters for a specific language code (Dutch,..) or A system like ICD-10 or ICPC-2,..

I have a query that is working and returns the above object when searching in 1 field (Descriptions.Term) that is the following:

working query

 {
  "query": {
    "bool": {
      "must": {
        "nested": {
          "inner_hits": {
            "highlight": {
              "fields": {
                "*": {}
              }
            }
          },
          "path": "Descriptions",
          "query": {
            "bool": {
              "should": [
                {
                  "multi_match": {
                    "fields": [
                      "Descriptions.Term",
                      "Descriptions.Term._2gram",
                      "Descriptions.Term._3gram"
                    ],
                    "query": "gebroken*~ n",
                    "type": "bool_prefix"
                  }
                }
              ],
              "filter": [
                {
                            "bool": {
                                "should": [
                                    {
                            "term": {
                                    "Descriptions.System": "ibui"
                                    }
                            },{
                            "term": {
                                    "Descriptions.System": "icd-10"
                                    }
                            },{
                            "term": {
                                    "Descriptions.System": "icpc-2"
                                    }
                            }
                                ],
                                "minimum_should_match": "1"
                                }
                            },
                
                {
                            "term": {
                                    "Descriptions.Active": "true"
                                    }
                            },
                {
                  "term": {
                    "Descriptions.LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee"
                  }
                }
              ]
            }
          }
        }
      }
      
    }
  }
}

But when we somethings need to search in multiple fields. When adding the Descriptions.Code field to the fields map the query is not working and I can't figure out why. I have it decleared inside my mapping so it should be searchable? I'm searching for the Code of the object above in both fields (Descriptions.Term & Descriptions.Code) but it doesn't returns the hit.

not working query

    {
  "query": {
    "bool": {
      "must": {
        "nested": {
          "inner_hits": {
            "highlight": {
              "fields": {
                "*": {}
              }
            }
          },
          "path": "Descriptions",
          "query": {
            "bool": {
              "should": [
                {
                  "multi_match": {
                    "fields": [
                      "Descriptions.Term",
                      "Descriptions.Term._2gram",
                      "Descriptions.Term._3gram",
                      "Descriptions.Code"
                    ],
                    "query": "10008220*~ n",
                    "type": "bool_prefix"
                  }
                }
              ],
              "filter": [
                {
                            "bool": {
                                "should": [
                                    {
                            "term": {
                                    "Descriptions.System": "ibui"
                                    }
                            },{
                            "term": {
                                    "Descriptions.System": "icd-10"
                                    }
                            },{
                            "term": {
                                    "Descriptions.System": "icpc-2"
                                    }
                            }
                                ],
                                "minimum_should_match": "1"
                                }
                            },
                
                {
                            "term": {
                                    "Descriptions.Active": "true"
                                    }
                            },
                {
                  "term": {
                    "Descriptions.LanguageId": "3d50c237-0add-43e7-92a2-5edf1ac7c6ee"
                  }
                }
              ]
            }
          }
        }
      }
      
    }
  }
}

mapping:

    {
    "settings": {
        "number_of_shards": 1,
        "analysis": {
            "analyzer": {
                "autocomplete": {
                    "tokenizer": "custom_tokenizer"
                }
            },
            "tokenizer": {
                "custom_tokenizer": {
                    "type": "ngram",
                    "min_gram": 2,
                    "max_gram": 6,
                    "token_chars": [
                        "letter",
                        "digit",
                        "symbol",
                        "punctuation"
                    ]
                }
            }
        },
        "max_ngram_diff" : "5"
    },
  "mappings": {
    "properties": {
      "Descriptions": {
        "type": "nested",
        "properties": {
          "Term": {
            "type": "search_as_you_type",
            "analyzer": "autocomplete"
          },
          "Code": {
            "type": "keyword",
            "index": true
          },
          "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
          }
        }
      }
    }
  }
}

Thank you for helping me out!

elasticsearch

search

autocomplete

mapping

0 Answers

Your Answer

Accepted video resources