# Filtering using terms for function\_score in elasticsearch?

**URL:** <https://discuss.elastic.co/t/filtering-using-terms-for-function-score-in-elasticsearch/235051>\
**Category:** Elasticsearch\
**Created:** [May 31, 2020, 7:10am UTC](https://discuss.elastic.co/t/filtering-using-terms-for-function-score-in-elasticsearch/235051 "2020-05-31T07:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijith\_chandran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhijith_chandran/32/60470_2.png) [@abhijith\_chandran](https://discuss.elastic.co/u/abhijith_chandran)\
**Post date:** [May 31, 2020, 7:10am UTC](https://discuss.elastic.co/t/filtering-using-terms-for-function-score-in-elasticsearch/235051/1 "2020-05-31T07:10:20Z")

</div>

I am trying to filter out results of a cosine similarity using `terms` . This is the query I am using?

```auto
{
    "size": 15,
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": {
                        "terms": {
                            "id_": [
                                "1","2", "999"
                            ]
                        }
                    }
                }
            }
        },
        "script_score": {
            "script": {
                "source": "cosineSimilarity(params.query_vector, 'emb_vector') + 1.0",
                "params": {
                    "query_vector": [

                    ]
                }
            }
        }
    },
    "_source": {
        "includes": [
            "title",
            "body",
            "id_",
            "text"
        ]
    }
}

```

What I want is only those docs with the id 1,2 or 999 be returned. But, it is throwing the following error:

```auto
"reason": "[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 17,
                "col": 9

```

What am I doing wrong?

Here is the mapping:

```auto
{"mappings": {
  "properties": {
    "id_": {
      "type": "text"
    },
    "emb_vector": {
      "type": "dense_vector",
      "dims": 768
    },
    "title": {
      "type": "text"
    }, 
    "text": {
      "type": "text"
    }

  }
}
}

```

---

<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:** [June 4, 2020, 2:05pm UTC](https://discuss.elastic.co/t/filtering-using-terms-for-function-score-in-elasticsearch/235051/2 "2020-06-04T14:05:40Z")

</div>

I would write your query like this:

```auto

  "query": {
    "script_score": {
      "query": {
        "terms": {
          "id_": [
            "1","2","999"
          ]
        },
        "script": {
          "source": "cosineSimilarity(params.query_vector, 'emb_vector') + 1.0",
          "params": {
            "query_vector": []
          }
        }
      }
    }
  }

```

The query you provided has incorrect syntax, as there are two query objects: `function_score` and `script_score` under a single `query`.

---

<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 2, 2020, 2:05pm UTC](https://discuss.elastic.co/t/filtering-using-terms-for-function-score-in-elasticsearch/235051/3 "2020-07-02T14:05:49Z")

</div>

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