Dynamic Facets in Elasticsearch

Hello All,

how to achieve dynamic faceting functionality in elastic search?

Is there a way my facets(not values inside the facets) change as per the search query?

I don't want to perform aggregation on any specific field to use as facet.

Instead, search query should be able to return the relevant facets.

thanks in advance.

I am not sure I understand what you are looking for. Could you provide an example? How do you expect the query to know or determine which facets that are relevant?

Is there a way to generate the facets based on the output from search query?

my goal is not to have a fixed facets, instead the facets will be changed based on the search results.

How? Please provide a real example.

Not that I am aware of, but then I do not understand what you are trying to achieve either.

Hi @Christian_Dahlqvist

Let's say, I have two indices medical data and financial data.

i want to make a generic search without pointing to any index.

first search word: cancer
can I get facets for medical data as it is related to medical data.

second search word: profit
similarly, can i get facets for financial data?

we are trying to develop a web app where multiple sources are indexed. so, just trying understand if this is possible. I have no idea how we can achieve this.

so far, what i have seen is, no matter how many sources are indexed, they all have common facets.

so, do i need to figure out the same ? finding common fields across all sources to make them facets?


The recommended solution to "relevant facets" was discussed in the comments section of this Youtube video.

It looks something like this:

	"query": {  // Your query here  },
	"aggs": {
		"top_matches": {
			"sampler": {"shard_size": 500},
			"aggs": {
				"relevant_facets": {
					"significant_terms": {
						"field": "department"

To explain what this does it's useful to visualise what the aggregations will tune into. Below is an example of all the departments found for a somewhat fuzzy query (combining phrase, ANDed and ORed terms)

The x-axis is match-quality score and the group of results on the far-right is the top-scoring results matching the full search phrase exactly while the left-side is the many low-quality results that match only one of the search terms. The bars are not to scale (number of matching docs) but are the same size because they show percentages of departments found in that score-band.

Note how the high-scoring matches come from a handful of departments while the low-quality matches come from many departments (not much better than taking a random sample of docs.).
The "sampler" aggregation will help focus analysis on the right hand side of this diagram and the significant_terms aggregation will help identify those departments skewed towards being in the right rather than the left.

1 Like

Re-reading your question - if the challenge is also not knowing which fields to show as well as which values then using copy_to in the mappings to a common field like "all_facets" could help. You would still use the example aggregation I gave, but on the "all_facets" field.

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