# Query documents with per term weights

**URL:** https://discuss.elastic.co/t/query-documents-with-per-term-weights/155648
**Category:** Elasticsearch
**Created:** [November 7, 2018, 2:09am UTC](https://discuss.elastic.co/t/query-documents-with-per-term-weights/155648 "2018-11-07T02:09:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lc\_es](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@lc\_es](https://discuss.elastic.co/u/lc_es)
#### Post date: [November 7, 2018, 2:09am UTC](https://discuss.elastic.co/t/query-documents-with-per-term-weights/155648/1 "2018-11-07T02:09:17Z")

</div>

The documents in my index have a field containing a list of all attribute terms, like  
`"fieldA": ["attr1", "attr3", "attr7"]`  
and for each attribute term, a field containing its weight, like  
`"weight:attr1": 0.5, "weight:attr3": 0.3, "weight:attr7": 0.8]`.

I am using a `multi_match` query on the `"fieldA"` right now and I want to incorporate these weights into the score, so that the score of any query, document pair can be  
`doc["weight:attr1"] * tf-idf(query, doc, "attr1") + doc["weight:attr2"] * tf-idf(query, doc, "attr2") + ...`.

I am wondering how I should construct this query.

I tried using `function_score` but since `function_score` is per query I have to explicitly list out all possible `"attr"`s at query time which is not ideal for me.

Thanks!

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [November 20, 2018, 10:46pm UTC](https://discuss.elastic.co/t/query-documents-with-per-term-weights/155648/2 "2018-11-20T22:46:19Z")

</div>

In Elasticsearch 7.x we will have [feature](https://www.elastic.co/guide/en/elasticsearch/reference/master/feature.html) and [feature vector](https://www.elastic.co/guide/en/elasticsearch/reference/master/feature-vector.html) data types, and together with [feature query](https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-feature-query.html) they will let you very efficiently process your request. You would be able to model your `fieldA` field as a `feature_vector` and run `feature_query` request on it.

---

<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: [December 18, 2018, 10:56pm UTC](https://discuss.elastic.co/t/query-documents-with-per-term-weights/155648/3 "2018-12-18T22:56:27Z")

</div>

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