Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple fields to a common terms query in ElasticSearch?

I have a common term query like so.

{
    "query" : {
        "common" : {
            "DocumentData.OCR_Text" : {
                "query" : "block 310 luis",
                "cutoff_frequency" : 0.001
            }
        }
    }
}

I want to search on 2 or more fields but this gives me an error.

{
    "query" : {
        "common" : {
            "Grantors" : {
                    "query" : "block 310 luis",
                    "cutoff_frequency" : 0.001
                },
            "DocumentData.OCR_Text" : {
                "query" : "block 310 luis",
                "cutoff_frequency" : 0.001
            }
        }
    }
}

nested: ElasticsearchParseException[Expected field name but got START_OBJECT "DocumentData.OCR_Text"];

How would you do this?

like image 591
Donny V. Avatar asked Feb 10 '23 21:02

Donny V.


1 Answers

You should wrap it in a Bool Query

{
    "query": {
        "bool": {
            "should": [
                {
                    "common": {
                        "Grantors": {
                            "query": "block 310 luis",
                            "cutoff_frequency": 0.001
                        }
                    }
                },
                {
                    "common": {
                        "DocumentData.OCR_Text": {
                            "query": "block 310 luis",
                            "cutoff_frequency": 0.001
                        }
                    }
                }
            ]
        }
    }
}
like image 76
Dan Tuffery Avatar answered Feb 13 '23 21:02

Dan Tuffery