trex February 2016

Elasticsearch bool must_not doesn't work with multi-fields.

I use multi-fields https://www.elastic.co/guide/en/elasticsearch/reference/current/_multi_fields.html

Here is part of my mapping:

...
"Diagnosis": {
               "type": "string",
               "fields":{
                 "not_analyzed":{
                    "type":"string",
                    "index":"not_analyzed"
                 }
                }
            }
...

I run this query:

curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search?pretty' -d '{"query": {"bool": {"must_not": [{"match": {"Diagnosis.not_analyzed": "F06.4 - Organic anxiety disorder"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

Despite the must_not clause the query above returns "F06.4 - Organic anxiety disorder" string amid other results.

I can exclude all results with 'anxiety' word doing this

curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search?pretty' -d '{"query": {"bool": {"must_not": [{"match": {"Diagnosis": "anxiety"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

But the goal is to exclude only the exact string "F06.4 - Organic anxiety disorder" from results. How can I do that?

Answers


Richa February 2016

If you want to exclude exact String use term query like .

curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search? pretty' -d '{"query": {"bool": {"must_not": [{"term": {"Diagnosis.not_analyzed": "F06.4 - Organic anxiety disorder"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

Hope this helps


AlainIb February 2016

Try this 1) Change the mapping to lower case the data but don't cut them by word (default mapping )

curl -XPUT 'localhost:9200/...../' -d '{
 "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "keylower":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
     }
  },
  "mappings":{
        "specimens" : {
            "_all" : {"enabled" : true},
            "_index" : {"enabled" : true},
            "_id" : {"index": "not_analyzed", "store" : false},
            "properties" : {

                "Diagnosis" : {"type" : "string",   "store" : "yes","index": "not_analyzed" } 

            }
        }
    }
}

2) this will return only data not containing "Organic anxiety disorder" (where * can be any words)

{
    "query" : {
        "bool" : {
            "must_not" : [{
                    "wildcard" : {
                        "Diagnosis" : {
                            "value" : "*organic anxiety disorder*"
                        }
                    }

                }
            ]
        }
    }
}   

3) use a strict search to exclude data :

{
    "query" : {
        "bool" : {
            "must_not" : [{
                    "term" : {
                        "Diagnosis.not_analyzed" : "f06.4 - organic anxiety disorder"
                    }
                }
            ]
        }
    }
} 

Post Status

Asked in February 2016
Viewed 1,428 times
Voted 10
Answered 2 times

Search




Leave an answer