# Calculate term co-occurrence matrix

**URL:** https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287
**Category:** Elasticsearch
**Created:** [December 17, 2014, 9:02am UTC](https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287 "2014-12-17T09:02:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Aeneas\_Wiener](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aeneas_wiener/32/1042_2.png) [@Aeneas\_Wiener](https://discuss.elastic.co/u/Aeneas_Wiener)
#### Post date: [December 17, 2014, 9:02am UTC](https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287/1 "2014-12-17T09:02:56Z")

</div>

Hi,

Is there an efficient way to calculate a term co-occurrence matrix  
[http://lucene.472066.n3.nabble.com/Calculate-Term-Co-occurrence-Matrix-td1226990.html](http://lucene.472066.n3.nabble.com/Calculate-Term-Co-occurrence-Matrix-td1226990.html)  
(i.e. a matrix showing how often a pair of terms of occurred in the same  
document.)

I tried fetching a list of relevant terms using a terms aggregation, and  
then I have two nested loops to calculate co-occurence of these terms via a span  
near query  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html).  
Is there a faster way to do this in elasticsearch?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fe184e70-4343-4a16-b941-0f31de19064e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fe184e70-4343-4a16-b941-0f31de19064e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Hossein\_Nasr\_Esfahan](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@Hossein\_Nasr\_Esfahan](https://discuss.elastic.co/u/Hossein_Nasr_Esfahan)
#### Post date: [May 2, 2017, 5:15am UTC](https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287/2 "2017-05-02T05:15:13Z")

</div>

Hi Aeneas,

I was wonder if you have found any efficient way to solve this problem or not?

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: [May 2, 2017, 7:09am UTC](https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287/3 "2017-05-02T07:09:31Z")

</div>

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html)

---

<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 5, 2017, 10:00pm UTC](https://discuss.elastic.co/t/calculate-term-co-occurrence-matrix/21287/4 "2017-07-05T22:00:45Z")

</div>


