Term Query not fetching any data


(Pradeep Nair) #1

Hi ,

I referred the link "https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html" and i am trying to fetch all documents based on some status.

Mapping of the field I am searching for:
"documentStatus": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

Query that I am using:
{
"query": {

    "terms" : { "documentStatus" : ["New","Updated","Action Needed"]}
}

}
======and the below query=========
{
"query": {

    "term" : { "documentStatus" : "New"}
}

}

The term query response has no document, however if i change to match it gives me the result. However, I have certain statuses that have 2 words ("Action Needed"). I would like to search for the exact term.

Can you please help me?
Thanks in advance.


#2

For the exact matches with the Terms query, you need to use the keyword version of your field :
documentStatus.keyword


(Pradeep Nair) #3

Thanks a lot Klof. It indeed works. I have a follow up question, I have another field with the same mapping. And it is a id field numeric values in String. For this field the term query works without the keyword version. Can you please help me understand how it works in this case?

Thanks again. It was super quick.


#4

Yes because Numeric datatypes are not analyzed, so if your field is define as an integer or float you will be able to Terms query on exact match.
In your mapping, you could also define the type of the documentStatus field as a keyword, if you know that you will always query by "exact match". And in that case, no need to define another version of the field.


(Pradeep Nair) #5

Hi Klof,

Thanks. I do understand if the field is a number then term and match queries can be interchanged for the same result (may be performance of the query would be different). However, like i said the field is a number stored as a string in ES.
The mapping for the id field is as shown below:

"documentID": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

Sample values for the ID are "14360029", "14368494".

The query works fine.
{
"query": {

    "terms" : { "documentID" : ["14360029","14368494"]}
}

}

Does your explanation holds true even in this case? That because the field documentID is a number represented as a string, somehow ES understands this is a numeric value and handles it like a number and not a string?

Thanks once again.


#6

Oh ok, no not really, indeed it's a text but when you query on documentID or documentStatus with the text type, it's the standard analyzer (default tokenizer + lowercase) which is applied, if there's no custom analyzer.
So that said, the values will be indexed like :

Data:
documentID: 14360029
documentStatus: New

Indexed in ES:
documentID: 14360029
documentStatus: new

That's the reason why, your terms query on documentID is working, because after having been analyzed the id is exactly the same.

You could try to understand the logic :
"term" : { "documentStatus" : "new"}
instead of
"term" : { "documentStatus.keyword" : "New"}


(Pradeep Nair) #7

Thanks a ton Klof. That explains a lot. Yes it is default tokenizer and lowercase. And the terms query works with the status too. That was very helpful.


(system) #8

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