# Significant term aggregation on nested query

**URL:** https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392
**Category:** Elasticsearch
**Created:** [May 11, 2017, 12:29pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392 "2017-05-11T12:29:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![geppo](https://avatars.discourse-cdn.com/v4/letter/g/3be4f8/32.png) [@geppo](https://discuss.elastic.co/u/geppo)
#### Post date: [May 11, 2017, 12:29pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/1 "2017-05-11T12:29:52Z")

</div>

In my index I have indicized 2 times a text: the first field, called keywordtext, represents the entire text as keyword datatype, the second one, called sentences, is a nested datatype with the text divided into sentences and every sentence has his start time and end time. I would like to query a term into my dataset and then I would like to make significant query aggregation to retrieve the words correlated to my searched term. In Es 2.x I could make this operation making the entire text field a string datatype with termvector active. With Es 5.x I try to make a fulltext query against the nested field and then make a significant term aggregation, but it seems not working without errors:

```
        "query": {
  "bool": {
     "must": [
        {
           "nested": {
              "path": "sentences",
              "query": {
                 "match": {
                    "sentences.value": {
                       "query": "stato"
                    }
                 }
              }
           }
        },
        {
           "range": {
              "date": {
                 "from": "19/09/1989",
                 "to": "14/02/2017",
                 "format": "dd/MM/yyyy"
              }
           }
        }
     ]
  }
 },
 "aggs": {
  "frequentTerms": {
     "significant_terms": {
        "field": "keywordtext"
     }
  }
 }
}

```

How i can make the same operation with this version of elastic?

---

<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: [May 11, 2017, 12:51pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/2 "2017-05-11T12:51:45Z")

</div>

Try wrapping in the `nested` agg:

```
DELETE test
PUT test
{
  "settings": {
	"number_of_shards": 1,
	"number_of_replicas": 0
  }, 
  "mappings": {
	"doc": {
	  "properties": {
		"play": {
		  "type": "keyword"
		},
		"author": {
		  "type": "keyword"
		},
		"sentences":{
		  "type":"nested",
		  "properties": {
			"text":{
			  "type":"text",
			  "fielddata":true
			}
		  }
	  
		}
	  }
	}
  }
}
POST /test/doc/_bulk
{"index": {}}
{"sentences" : [{"text" : "filler"}]}
{"index": {}}
{"sentences" : [{"text" : "filler"}]}
{"index": {}}
{"sentences" : [{"text" : "filler"}]}
{"index": {}}
{"sentences" : [{"text" : "filler"}]}
{"index": {}}
{"sentences" : [{"text" : "filler"}]}
{"index": {}}
{"sentences" : [{"text" : "filler"}]}

POST test/doc
{
  "play": "macbeth",
  "author": "shakespeare",
  "sentences": [
	{
	  "text": "funny - there's a knife missing from the dishwasher"
	},
	{
	  "text": "where'd you get that knife?"
	},
	{
	  "text": "put the knife down - you'll hurt someone you bloody fool"
	}

  ]
}
GET test/_search
{
  "query": {
	"match": {
	  "author": "shakespeare"
	}
  },
  "size": 0,
  "aggs": {
	"shakespeare keywords": {
	  "nested": {
		"path": "sentences"
	  },
	  "aggs": {
		"foo": {
		  "significant_terms": {
			"field": "sentences.text"
		  }
		}
	  }
	}
  }
}
```

---

<div class="post-metadata">

### Author: ![geppo](https://avatars.discourse-cdn.com/v4/letter/g/3be4f8/32.png) [@geppo](https://discuss.elastic.co/u/geppo)
#### Post date: [May 11, 2017, 1:26pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/3 "2017-05-11T13:26:29Z")

</div>

Thanks for the answers but I would like to avoid fielddata, for the RAM consumption. I have to manage a very large dataset

---

<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: [May 11, 2017, 1:47pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/4 "2017-05-11T13:47:11Z")

</div>

> [@geppo](#):
>
> I would like to avoid fielddata, for the RAM consumption.

Funny you should mention that. I'm currently working on adding exactly that in the form of a new [significant\_text aggregation](https://github.com/elastic/elasticsearch/issues/23674)

Unlike significant\_terms it does not rely on fielddata and can strip out noisy repeated text that otherwise skews stats.  
The bad news for you I suspect is that it will not work on nested docs.

Cheers  
Mark

---

<div class="post-metadata">

### Author: ![geppo](https://avatars.discourse-cdn.com/v4/letter/g/3be4f8/32.png) [@geppo](https://discuss.elastic.co/u/geppo)
#### Post date: [May 11, 2017, 2:06pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/5 "2017-05-11T14:06:02Z")

</div>

Thanks Mark, That's a good news ! Your current work is very interesting for my job, also the representation of dbpedia entities on graph, last week, was very useful. Have you a blog where I can follow you?

---

<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: [May 11, 2017, 2:10pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/6 "2017-05-11T14:10:20Z")

</div>

> [@geppo](#):
>
> Your current work is very interesting for my job, also the representation of dbpedia entities on graph, last week, was very useful

Good to hear. Please add comments to the github issue for significant\_text if you have any suggestions.

> [@geppo](#):
>
> Have you a blog where I can follow you?

I don't have a blog but I have various demos on a [Youtube channel](https://www.youtube.com/channel/UC_Ci-rzrOoJsId8TgTs-8_g)  
I hope to add a video on the new significant\_text agg once it's merged into the master branch.

---

<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: [June 8, 2017, 2:22pm UTC](https://discuss.elastic.co/t/significant-term-aggregation-on-nested-query/85392/7 "2017-06-08T14:22:51Z")

</div>

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