Best query to only return documents matching a nested unique id 'id' and not the ES _id field?

Hey all,

I've been pulling my hair out a bit with this.

We are running a basic query which matches a set of unique id's - this works perfectly when querying the ES _id field.

But when we try to match a nested id field, using the exact same process (_source.id) - it just returns an empty match.

Any guidance appreciated!

Gentle bump!

I've tried match and also terms, e.g.:

{
  "query": {
    "terms": {
      "id": [ "...", "..." ]
    }
  }
}

I just can't get it to return any results! Pulling my hair out a little. When we used the standard ES _id field, it works fine.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Here I suspect a problem with mapping, analyzers....

1 Like

sure!

I have tried this, which I expect to work as an alternative to match:

GET index/_search
{
  "query": {
"terms": {
  "_source.id": [ "...", "..." ]
}
  }
}

And also:

GET index/_search
{
  "query": {
"terms": {
  "id": [ "...", "..." ]
}
  }
}

I have tried around 10/11 variations to try to get this to work!

I understood what you tried. What I'm asking for is a script I can run myself which exactly reproduces your problem.

Here you are not even sharing the id you're searching for so we can only make wild guesses.

One supposition for now. Try to lowercase the id. But again, no reproduction = hard to help.

Apologies @dadoonet - I'm only newly taking over from an external dev and I'm not a developer by trade, just relatively good at picking technical things up.

I'm not sure I follow how else to provide a script for you (aside for including the ids). For you to reproduce locally, you would need our actual ES endpoint + access to the endpoint? Is that what you are asking?

I also just tried lowercasing the id, but that returned the same response, unfortunately.

No. I don't want/need to access your machine fortunately.

I just need something I can copy and paste in Kibana dev console, then press run.

I can then find what your problem is and how to fix it.
If you read the link I shared, there's a practical example of a reproduction script.

At the very least share an id you are searching for.

Found the solution.

For anyone with a similar issue reading this, I just replaced "_source.id" with "id.keyword" and it worked.

Which is basically what I suspected when I wrote:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.