5

Am trying to fetch documents from elasticsearch with multiple fields using AND operator

for the below query am expecting the following results

AB-7000-8002-W

But am getting this error message Unrecognized token 'get': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@40d2a7e8; line: 1, column: 5]

 get my_index12/_search {
    "query" : {
        "bool": {
            "should": [
                {
                    "match": {
                        "code": {
                         "query": "AB-5000-6002-AK",
                         "operator": "and"
                        }
                    }
                },
                {
                    "match": {
                        "locale": {
                         "query": "en_US",
                         "operator": "and"
                        }
                    }
                }
            ]
        }
    }
 }

Please find my index documents below

 {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "code": "AB-7000-8002-W",
          "locale": "en_US"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "4",
        "_score": 1,
        "_source": {
          "code": "AB-7000-8002-W",
          "locale": "en_EU"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "code": "sG66tsdF",
          "locale": "en_US"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "3",
        "_score": 1,
        "_source": {
          "code": "AB-7000-6002-WK",
          "locale": "en_EU"
        }
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109

2 Answers2

9

Just move curly brace in the line get my_index12/_search { to the next line. It should work.

In order to get results which satisfy both the conditions, you have to use must clause instead of should. "AND" operator in match query is not meant for the use case you want to achieve. Use below query.

{
"query": {
"bool": {
  "must": [
    {
      "match": {
        "code": {
          "query": "TE-7000-8002-W",
          "operator": "and"
        }
      }
    },
    {
      "match": {
        "locale": {
          "query": "en_US",
          "operator": "and"
          }
        }
      }
     ]
    }
  }
}
Richa
  • 7,419
  • 6
  • 25
  • 34
  • Yes, its working., but its bring all the records which containing `en_US`, actually it should compare with both the fields `code & locale` with AND operator – Karthikeyan Jun 13 '18 at 09:36
2

For those looking for AND like query on multiple fields should work . Below will search for code as "TE-7000-8002-W" and locale as en_US only

    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                       "code": "TE-7000-8002-W"
                    }
                },
                {
                    "match": {
                       "locale": "en_US"
                    }
                }
            ]
        }
    }
}
Ganesh
  • 107
  • 1
  • 15