Elastic Search Notes
  • Introduction
  • Full Text
  • Aggregations
    • Facets Vs Aggregations
  • Filters
  • References
  • Tools
  • Query DSL
    • Query and filter context
      • Query context
      • Filter context
      • Filtered Vs filter
      • termQuery vs. termFilter
    • Term level queries vs full text queries
  • Parent Child Relationship
  • Nodes
    • Master Node
  • Elastic Search Networking
  • Logstash
    • HighAvailability
    • Grok patterns
  • KeyConcepts
    • Shard
    • Segment
    • Translog
  • Document CRUD operations
    • Updates
  • adv topics
  • Relevance
    • BM25 Scoring Formula
  • Named Queries
Powered by GitBook
On this page

Was this helpful?

  1. Query DSL
  2. Query and filter context

termQuery vs. termFilter

PreviousFiltered Vs filterNextTerm level queries vs full text queries

Last updated 5 years ago

Was this helpful?

Which query should I prefer? This filter query:

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("someField", "someValue")));

or this termQuery:

srb.setQuery(QueryBuilders.termQuery("someField", "someValue"));

Use a term query if the terms you are looking for should affect the _score (relevance). If it should just exclude all docs except for those that match, use a term filter.

Historically, queries and filters were separate components in Elasticsearch. Starting in Elasticsearch 2.0, filters were technically eliminated, and all queries gained the ability to become non-scoring.

However, for clarity and simplicity, we will use the term "filter" to mean a query which is used in a non-scoring, filtering context. You can think of the terms "filter", "filtering query" and "non-scoring query" as being identical.

Similarly, if the term "query" is used in isolation without a qualifier, we are referring to a "scoring query".

https://discuss.elastic.co/t/termquery-vs-termfilter/4839
https://www.elastic.co/guide/en/elasticsearch/guide/current/_queries_and_filters.html