# Query and score similar documents based on hierarchical data

**URL:** <https://discuss.elastic.co/t/query-and-score-similar-documents-based-on-hierarchical-data/189608>\
**Category:** Elasticsearch\
**Created:** [July 9, 2019, 6:22pm UTC](https://discuss.elastic.co/t/query-and-score-similar-documents-based-on-hierarchical-data/189608 "2019-07-09T18:22:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Borwick\_5576](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/borwick_5576/32/44759_2.png) [@Borwick\_5576](https://discuss.elastic.co/u/Borwick_5576)\
**Post date:** [July 9, 2019, 6:22pm UTC](https://discuss.elastic.co/t/query-and-score-similar-documents-based-on-hierarchical-data/189608/1 "2019-07-09T18:22:53Z")

</div>

I'm working in creating and simplifying probabilistic topic models for large corpora of data. Every document I would index will contain a field having the following format:

> ...  
> "topics" : {  
> "level0" : ["keywords"],  
> "level1" : ["keywords"],  
> "level2" : ["keywords"]  
> }  
> ...

I would want to make a query that, given a document id (let us call this doc **D** ), would give me the documents (let us call all possible hits **H** ) which are similar based on this field. In order to get a match, one of all level keywords from **D** has to be present in any of the levels. Then, the score of each hit should be higher if the keyword they share is at a lower level in **D**. It's should get higher if the keyword they share is at a lower level in **H**.

I'm currently using the following query

```
"query": {
	"bool" : {
		"should" : [
	{
		"multi_match" : {
			"boost" : 3,
			"query": "Keyword_A", 
			"fields": ["topics.l0", "topics.l1", "topics.l2"] 
	} },
	{
		"multi_match" : {
			"boost": 2,
			"query": "Keyword_B", 
			"fields": ["topics.l0", "topics.l1", "topics.l2"] 
	} },
	{
		"multi_match" : {
		"query": "Keyword_C", 
		"fields": ["topics.l0", "topics.l1", "topics.l2"] 
	}
	}
			
		]
	}
	
} #for now, Keywords_[A, B, C] are taken from **D** manually as I don't know how to fetch this fields directly into a query

```

In combination with index boost in each of the field.

Is there a better way for me to define this query or the score?

Thanks in advance

---

<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:** [August 6, 2019, 6:22pm UTC](https://discuss.elastic.co/t/query-and-score-similar-documents-based-on-hierarchical-data/189608/2 "2019-08-06T18:22:53Z")

</div>

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