# Significant\_text sorting

**URL:** <https://discuss.elastic.co/t/significant-text-sorting/186463>\
**Category:** Elasticsearch\
**Created:** [June 19, 2019, 12:17pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463 "2019-06-19T12:17:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![slovan9](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@slovan9](https://discuss.elastic.co/u/slovan9)\
**Post date:** [June 19, 2019, 12:17pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/1 "2019-06-19T12:17:50Z")

</div>

Hello,

Im new to Elasticsearch and I cannot solve following issue. I used _sampler_ and _significant\_text_ aggregations to find most popular words in my dataset and it worked very well. However, I would like to order the created buckets by their doc\_count in a descending (or asc) manner. And that is the issue, I’m not able to find proper solution to do it.

I tried “order” command but without success (and many more). Is it even possible to sort buckets when I used _sampler_ and _significant\_text_ aggregations? E.g. sorting worked with term aggregation but not with significant\_text. Any ideas? Thanks a lot in advance for any hand.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 19, 2019, 3:28pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/2 "2019-06-19T15:28:52Z")

</div>

“Most popular” is not usually a desirable sort order for text because it would generally give you words like “the”, regardless of whatever your query was. That said, if you really want that behaviour you can use a custom scripted scoring heuristic to rank on either foreground or background count.

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 20, 2019, 9:43am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/3 "2019-06-20T09:43:13Z")

</div>

Mark I have the same problem.  
You share your proposed approach in code.  
I have another question, kibana dasboard also has significant terms but not significant text. How do I make significant text graph in visualize dashboard.

Thanks

---

<div class="post-metadata">

**Author:** ![slovan9](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@slovan9](https://discuss.elastic.co/u/slovan9)\
**Post date:** [June 20, 2019, 12:20pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/4 "2019-06-20T12:20:28Z")

</div>

Dear Mark,  
Thanks a lot for prompt response. I would like to ask you If is possible to provide example code or just snippet of your proposed solution. Thanx in advance.

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 20, 2019, 12:32pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/5 "2019-06-20T12:32:56Z")

</div>

![contentkibana](https://us1.discourse-cdn.com/elastic/original/3X/3/8/38904017a3fcababc358a0b68741a5263ee4a14e.png)

Order command not working.I also visualize how to add code on the dashboard to kibana.

Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 22, 2019, 10:04am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/6 "2019-06-22T10:04:16Z")

</div>

Don’t have access to a computer to give a full working snippet but the docs you need are [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_scripted)  
You’d use a painless script that just returned the superset\_freq for the background stats.  
Kibana does not have support for significant\_text currently - I expect the best thing to do is to open an issue on the kibana github issues asking for support to be added to something like the tag cloud visualization

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 22, 2019, 6:14pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/7 "2019-06-22T18:14:19Z")

</div>

```
"script_heuristic": {
          "script": {
        "lang": "painless",
        "source": "params._subset_freq/(params._superset_freq - params._subset_freq + 1)"
      }
        }

```

how to integrate this code into the aggs part ?

Other Question :  
Is there no way to visualize significant text code in kiban? if possible, can you share images?

Thanks for your answers

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 22, 2019, 8:08pm UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/8 "2019-06-22T20:08:53Z")

</div>

hey Mark by the way, Can we visualize significant text data in datatable ?  
that's enough, I don't need grafic.

Good Work

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 24, 2019, 8:24am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/9 "2019-06-24T08:24:12Z")

</div>

> [@mr\_searchng](#):
>
> how to integrate this code into the aggs part ?

Here's a full reproduction:

```
GET signalmediaexact/_search?q=h5n1
{
  "size": 0,
  "aggs": {
	"sample": {
	  "sampler": {
		"shard_size": 1000
	  },
	  "aggs": {
		"keywords": {
		  "significant_text": {
			"field": "content",
			"script_heuristic": {
			  "script": {
				"lang": "painless",
				"source": "params._superset_freq + 0f"
			  }
			}
		  }
		}
	  }
	}
  }
}

```

I added zero to get the script's numeric casting to work properly.  
As predicted - the most popular words in my sample are dull eg. the, and, of etc

> [@mr\_searchng](#):
>
> hey Mark by the way, Can we visualize significant text data in datatable ?

I suspect the answer is "no" - best to ask in the Kibana forum.

---

<div class="post-metadata">

**Author:** ![slovan9](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@slovan9](https://discuss.elastic.co/u/slovan9)\
**Post date:** [June 24, 2019, 9:19am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/10 "2019-06-24T09:19:25Z")

</div>

Thanks a lot Mark for your time, help and code. It works properly now.

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 25, 2019, 6:59am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/11 "2019-06-25T06:59:34Z")

</div>

doc\_count is not ranked.

Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 25, 2019, 7:40am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/12 "2019-06-25T07:40:30Z")

</div>

Maybe you’re looking to use subset frequency rather than superset in the script?

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 25, 2019, 8:44am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/13 "2019-06-25T08:44:17Z")

</div>

i want it sorted by doc\_count.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 25, 2019, 8:49am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/14 "2019-06-25T08:49:23Z")

</div>

Which? Foreground or background?  
"\_subset\_freq" is the former and "\_superset\_freq" is the latter.

---

<div class="post-metadata">

**Author:** ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)\
**Post date:** [June 25, 2019, 9:00am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/15 "2019-06-25T09:00:41Z")

</div>

It's\_subset\_freg working. And it would be perfect if we visualize it in Kiban.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 23, 2019, 9:00am UTC](https://discuss.elastic.co/t/significant-text-sorting/186463/16 "2019-07-23T09:00:49Z")

</div>

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