Execute a graphql query using painless script, with params having hyphen and space and match a field property with type keyword and text

Execute a graphql query using painless script, with params having hyphen and space and match a field property with type keyword and text


0

I’m new to graphql and opensearch queries. Here I’m trying to add a filter to existing query for city, my params inside the query are having data with – and spaces. While I’m trying to query it, data is getting filtered only with first word. For example, my params is having an array "cityNames" : ["chicago","new york", "santa-fe"]. Data gets filtered if I use "chicago", but for "new york" I get results only when I give "new" instead of "new york", and for "santa-fe" it doesnt return any data. How to get records from loaddata only for the cityNames matching params?

here are properties: [
"testCode" : {
"type": "keyword"
},
"cityName" : {
"type": "text",
"fielddata":true
},
]

    {
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "params": {
                 "testCodes": [
                  "T","R"
                ],
                "cityNames": [
                  "albuquerque-santa fe",
                  "chicago",
                  "new york"
                ]
              },
              "source": """ if(doc.containsKey('testCode') && !doc['testCode'].empty){      
                                if(params.testCodes.contains(doc['testCode'].value)){
                                    if(params.cityNames.contains(doc['cityName'].value)){
                                            return true;
                                            }
                                        }
                                    } """
            }
          }
        }
      ]
    }
  },
  "size": 10000
}

Data:

{
    "loadData": [
        {
            "testCode": "T",
            "id": "839294",
            "cityName": "ALBUQUERQUE-SANTA FE"
        },
        {
            "testCode": "T",
            "id": "839294",
            "cityName": "ALBUQUERQUE-SANTA FE"
        },
        {
            "testCode": "R",
            "id": "839294",
            "cityName": "ALBUQUERQUE-SANTA FE"
        },
        {
            "testCode": "T",
            "id": "839294",
            "cityName": "CHICAGO"
        },
        {
            "testCode": "R",
            "id": "839294",
            "cityName": "CHICAGO"
        }
        {
            "testCode": "R",
            "id": "839294",
            "cityName": "NEW YORK"
        },
        {
            "testCode": "R",
            "id": "839294",
            "cityName": "ATLANTA"
        },
        {
            "testCode": "T",
            "id": "839294",
            "cityName": "ATLANTA"
        }
    ]
}

Share
Improve this question


Load 5 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *