Dres
Dres

Reputation: 1509

Parsing Exception error when using Terms in ElasticSearch

I'm getting an error on this elastic search for terms. The error message is

"[parsing_exception] [terms] unknown token [START_ARRAY] after [activeIds], with { line=1 & col=63 }"

Active Ids is an array of unique ids. It sort of looks like

const activeIds = [ '157621a1-d892-4f4b-80ca-14feddb837a0',
'd04c5c93-a22c-48c3-a3b0-c79a61bdd923',
'296d40d9-f316-4560-bbc9-001d6f46858b',
'2f8c6c37-588d-4d24-9e69-34b6dd7366c2',
'ba0508dd-0e76-4be8-8b6e-9e938ab4abed',
'ab076ed9-1dd5-4987-8842-15f1b995bc0d',
'ea6b0cff-a64f-4ce3-844e-b36d9f161e6f' ]



let items = await es.search({
        "index": table,
        "body": {
          "from": 0, "size": 25,
          "query": {
            "terms" : {
                "growerId" : { 
                    activeIds
                }
            },
            "bool": {
                "must_not": [ 
                    { "match":  
                        { 
                            "active": false
                        }
                    },
                ],
                "must": [
                            { "query_string" : 
                                { 
                                    "query": searchQuery,
                                    "fields": ["item_name"] 
                                }
                            }
                        ],
                    }
                }
            }
        })

Appreciate the help!

Edit: Answering this question- "What's the expected result? Can you elaborate and share some sample data? – Nishant Saini 15 hours ago"

I'll try to elaborate a bit.

1) Overall I'm trying to retrieve items that belong to active users. There are 2 tables: user and items. So I'm initially running an ES that returns all the users that contain { active: true } from the user table

2) Running that ES returns an array of ids which I'm calling activeIds. The array looks like what I've already displayed in my example. So this works so far (let me know if you want to see the code for that, but if I'm getting an expected result then I don't think we need that now)

3) Now I want to search through the items table, and retrieve only the items that contain one of the active ids. So an item should look like:

ITEM

4) expected result is retrieve an array of objects that match the growerId with one of the activeIds. So if I do a search query for "flowers", a single expected result should look like:

[ { _index: 'items-dev',
_type: 'items-dev_type',
_id: 'itemId=fc68dadf-21c8-43c2-98d2-cf574f71f06d',
_score: 11.397207,
_source: 
{ itemId: 'fc68dadf-21c8-43c2-98d2-cf574f71f06d',
'@SequenceNumber': '522268700000000025760905838',
item_name: 'Flowers',
grower_name: 'Uhs',
image: '630b5d6e-566f-4d55-9d31-6421eb2cff87.jpg',
dev: true,
growerId: 'd04c5c93-a22c-48c3-a3b0-c79a61bdd923',
sold_out: true,
'@timestamp': '2018-12-20T16:09:38.742599',
quantity_type: 'Pounds',
active: true,
pending_inventory: 4,
initial_quantity: 5,
price: 10,
item_description: 'Field of flowers' } },

So here the growerId matches activeIds[1]

But if I do a search for "invisible", which is created by a an inactive user, I get:

[ { _index: 'items-dev',
_type: 'items-dev_type',
_id: 'itemId=15200473-93e1-477c-a1a7-0b67831f5351',
_score: 1,
_source: 
{ itemId: '15200473-93e1-477c-a1a7-0b67831f5351',
'@SequenceNumber': '518241400000000004028805117',
item_name: 'Invisible too',
grower_name: 'Field of Greens',
image: '7f37d364-e768-451d-997f-8bb759343300.jpg',
dev: true,
growerId: 'f25040f4-3b8c-4306-9eb5-8b6c9ac58634',
sold_out: false,
'@timestamp': '2018-12-19T20:47:16.128934',
quantity_type: 'Pounds',
pending_inventory: 5,
initial_quantity: 5,
price: 122,
item_description: 'Add' } },

Now that growerId does not match any of the ids in activeIds.

5) Using the code you helped with, it's returning 0 items.

Let me know if you need more detail. I've been working on this for a bit too long :\

Upvotes: 0

Views: 1588

Answers (1)

Nishant
Nishant

Reputation: 7874

Terms query accept array of terms so the terms query should be defined as below:

  "terms": {
    "growerId": activeIds
  }

You might face other errors as well after making the above correction. So below is full query which might help you:

{
  "from": 0,
  "size": 25,
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "active": false
          }
        }
      ],
      "must": [
        {
          "query_string": {
            "query": searchQuery,
            "fields": [
              "item_name"
            ]
          }
        },
        {
          "terms": {
            "growerId": activeIds
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions