Search code examples
elasticsearchquerydsl

How to query on a specific Elasticsearch type and fetch records matching a keyword?


I have built the following ElasticSearch (POST) query in order to fetch records matching will following criterias : -select type : "server" -select fields : "servername", "silo", "packages.displayname", "packages.displayversion","environment"

{
    "size": 10,
    "_source" : ["servername", "silo", "packages.displayname", "packages.displayversion","environment"],
    "query": {
          "type" : {
            "value" : "server"
        }
    }
}

It works !

The problem is when I want to add the following condition : -"packages.displayname" = "Google Chrome"

{
    "size": 10,
    "_source" : ["servername", "silo", "packages.displayname", "packages.displayversion","environment"],
    "query": {
          "type" : {
            "value" : "server"
        },
        "match" : {"packages.displayname" : "Google Chrome"}
    }
}

It doesn't work ! the following error is raised :

"type": "parsing_exception",
                "reason": "[type] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 8,
                "col": 9

I'm totally noob in ElasticSearch..can somebody help me to figure out how I should write the Query please ?

Thanks


Solution

  • You have to build a bool query:

    {
      "query": {
        "bool": {
          "must": {
            "match": {
              "packages.displayname": "Google Chrome"
            }
          },
           "must": {
            "type": {
              "value": "server"
            }
          }
        }
      }
    }