Meg
Meg

Reputation: 98

Elastic search - return documents which have strings other than a specific string

I want to return those documents which have any other values in infoList other than the one's starting from "cake". Eg: This is the complete data:

  {   "brand" : "A",
      "productType" : "food",
      "infoList" : [
        "pizza-34","cake-qaw-34"
      ]},
   {
      "brand" : "B",
      "productType" : "food",
      "infoList" : [
        "pasta-3"
      ]},
   {
      "brand" : "c",
      "productType" : "food",
      "infoList" : [
        "cake-233"
      ]}

After running the elastic search query, I should get these data only because they have other values (pizza-34, pasta-3) which do not begin with "cake". If there is any document that has only cake then it should not be returned.

{   "brand" : "A",
      "productType" : "food",
      "infoList" : [
        "pizza-34","cake-qaw-34"
      ]},
   {
      "brand" : "B",
      "productType" : "food",
      "infoList" : [
        "pasta-3"
      ]},

Upvotes: 0

Views: 338

Answers (1)

Kaveh
Kaveh

Reputation: 1310

In Elasticsearch, there is no dedicated array data type. Any field can contain zero or more values by default, however, all values in the array must be of the same data type more information here. Due to this there is no way to distinguish between elements in Array data type. So if you run this query:

Query:

GET myindex2/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "prefix": {
            "infoList": "cake"
          }
        }
      ]
    }
  }
}

The result will look like this:

Result:

"_source" : {
          "brand" : "B",
          "productType" : "food",
          "infoList" : [
            "pasta-3"
          ]
}

As you can see you will only get the document that doesn't have cake and document with "pizza-34","cake-qaw-34" will not returned as Elastic see the whole array as a single string. If you want to get results that you want you will need to query from Elasticsearch and then filter the result.

Upvotes: 1

Related Questions