Term query return no document

Hi,

I am trying to load a document by having the value of specific column. It loads the document once I use match query but doesn't load the document when I use term query.

GET /keydate/_search
{
  "query": {
"bool": {
  "filter": {
    "term": {
      "keyDateId": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
    }
  }
}
  }  
}

GET /keydate/_search
{
  "query": {
"bool": {
  "must": [
    { "match": {
      "keyDateId": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
    }}  
  ]
}
  }  
}

Is there any issue in my query?
Thanks

1 Like

Hi Ali,

You need to create keyDateId with both text and keyword analyzers.

PUT keydate/
{
     "mappings": {
    "_doc": {
      "properties": {
        "keyDateId": {
          "type":  "text" 
        },
        "keyDateId": {
          "type":  "keyword" 
        }
      }
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

Hi,

based on the documentation, my query should be fine. KeyDateId is keyword and not being analayzed .

right?

If that match query returns a result, and the term query doesn't, then keyDateId is most likely an analyzed field. Can you post the mapping of the index that you're querying (the output of GET keydate)

Here is my keydate index:

  {
  "keydate": {
    "aliases": {},
    "mappings": {
      "keydate": {
        "properties": {
          "address": {
            "properties": {
              "city": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "country": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "descriptor": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "objectState": {
                "type": "long"
              },
              "state": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetLine1": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetLine2": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "zipCode": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "classId": {
            "type": "keyword"
          },
          "createdBy": {
            "type": "keyword"
          },
          "createdOn": {
            "type": "date",
            "format": "date_optional_time"
          },
          "date": {
            "type": "date",
            "format": "date_optional_time"
          },
          "description": {
            "type": "text"
          },
          "endTime": {
            "type": "keyword"
          },
          "keyDateId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "keydateId": {
            "type": "keyword"
          },
          "locationAddress": {
            "properties": {
              "city": {
                "type": "keyword"
              },
              "country": {
                "type": "keyword"
              },
              "state": {
                "type": "keyword"
              },
              "streetLine1": {
                "type": "text"
              },
              "streetLine2": {
                "type": "text"
              },
              "zipCode": {
                "type": "keyword"
              }
            }
          },
          "message": {
            "type": "keyword"
          },
          "modifiedBy": {
            "type": "keyword"
          },
          "modifiedOn": {
            "type": "date",
            "format": "date_optional_time"
          },
          "objectState": {
            "type": "long"
          },
          "photoes": {
            "properties": {
              "url": {
                "type": "keyword"
              }
            }
          },
          "pinToTop": {
            "type": "boolean"
          },
          "reminder": {
            "properties": {
              "value": {
                "type": "integer"
              }
            }
          },
          "scheduleDate": {
            "type": "date",
            "format": "date_optional_time"
          },
          "schoolId": {
            "type": "keyword"
          },
          "sendNow": {
            "type": "boolean"
          },
          "sendText": {
            "type": "boolean"
          },
          "startTime": {
            "type": "keyword"
          },
          "title": {
            "type": "text"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1522271916303",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "5cophHReQIapRS0_H87tYg",
        "version": {
          "created": "6000199"
        },
        "provided_name": "keydate"
      }
    }
  }
}

Alright, the keyDateId field has been mapped as a text field, which is analyzed:

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

That's why your term query does not return any hits: the term query does not analyze the search terms, so there's a mismatch between your query and the way the field in the document has been indexed.

What you want to do is query the .keyword multi-field instead. This is a second way the keyDateId field has been indexed: as an non-analyzed field of type keyword. Instead of keyDateId you'd provide keyDateId.keyword:

GET /keydate/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "keyDateId.keyword": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
        }
      }
    }
  }
}
2 Likes

Thank you so much.

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