Search code examples
elasticsearchelasticsearch-2.0

Elastic Search "must" in array


I'm a newbie with Elastic Search.

I've got the next object indexed in elasticsearch:

                "doc": [
                    {
                        "partes": [
                            {
                                "algo": [
                                    {
                                        "Category": "Therapeutic or Preventive Procedure",
                                        "Neg": "false",
                                        "CandidatePreferred": "Obstetric Surgical Procedures",
                                        "CandidateScore": "1000",
                                        "CandidateMatched": "Obstetric Surgical",
                                        "Phrase": "OBSTETRIC SURGICAL",
                                        "CUI": "C0038906"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "partes": [
                            {
                                "algo": [
                                    {
                                        "Category": "Intellectual Product",
                                        "Neg": "false",
                                        "CandidatePreferred": "Given name",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "given",
                                        "Phrase": "given of discharge",
                                        "CUI": "C3244317"
                                    },
                                    {
                                        "Category": "Body Substance",
                                        "Neg": "false",
                                        "CandidatePreferred": "Discharge, body substance",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "Discharge",
                                        "Phrase": "given of discharge",
                                        "CUI": "C2926602"
                                    }
                                ]
                            },
                            {
                                "algo": [
                                    {
                                        "Category": "Health Care Activity",
                                        "Neg": "false",
                                        "CandidatePreferred": "Patient Discharge",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "Discharge",
                                        "Phrase": "given of discharge",
                                        "CUI": "C0030685"
                                    },
                                    {
                                        "Category": "Intellectual Product",
                                        "Neg": "false",
                                        "CandidatePreferred": "Given name",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "given",
                                        "Phrase": "given of discharge",
                                        "CUI": "C3244317"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }

My objective is get the elements where i have two CUIs in the same element algo, i.e. there is an algo in the doc who have both CUI's: C3244317 and C2926602.

I'm trying to make the next search:

{
    "query": {        
        "nested": {
            "path": "doc",
            "query": {
                "nested":{
                    "path":"doc.partes",
                    "query": {
                        "nested": {
                            "path":"doc.partes.algo",
                            "query": {
                                "bool": {
                                    "must": [
                                        { "term": { "doc.partes.algo.CUI": "C3244317" }},
                                        { "term": { "doc.partes.algo.CUI": "C2926602" }}
                                    ]

                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

But I haven't got any results:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

I've got results with should instead of must, but it's not the behaviour I've been searching for.


Solution

  • The default standard analyzer drops most punctuation, breaks up text into individual words, and lower cases them.

    So probably C3244317 is indexed as c3244317, i.e., with lowercase 'c'.

    You don't need such a large query. This should work fine:

    {  
       "query":{  
          "bool":{  
             "must":[  
                {  
                   "term":{  
                      "doc.partes.algo.CUI":"c3244317"
                   }
                },
                {  
                   "term":{  
                      "doc.partes.algo.CUI":"c2926602"
                   }
                }
             ]
          }
       }
    }