# Querying dense\_vector from the High Level REST Java API

**URL:** <https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956>\
**Category:** Elasticsearch\
**Created:** [August 14, 2020, 12:03am UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956 "2020-08-14T00:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJB](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MJB](https://discuss.elastic.co/u/MJB)\
**Post date:** [August 14, 2020, 12:03am UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/1 "2020-08-14T00:03:33Z")

</div>

All the examples I can find for querying dense vector mention script queries, eg. [Searching with Dense Vector](https://discuss.elastic.co/t/searching-with-dense-vector/213374)  
However, I have no idea on how to use those with the HL API.  
Is this possible at all and if yes then can you ES wizards give an example?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 14, 2020, 11:19am UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/2 "2020-08-14T11:19:21Z")

</div>

You can use `QueryBuilders.scriptScoreQuery()` and create a script score query with that and then execute a search? is that what you are after?

---

<div class="post-metadata">

**Author:** ![MJB](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MJB](https://discuss.elastic.co/u/MJB)\
**Post date:** [August 14, 2020, 4:06pm UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/3 "2020-08-14T16:06:40Z")

</div>

That's it, however I would appreciate an example on how to translate the example vector query from the docs to the Java HL REST API. I haven't been able to find one for Java, although there are examples for other languages.

```
{
  "size" : 5,
  "query": {
    "script_score": {
      "query" : {
        "match_all" : {}
      },
      "script": {
        "source": "cosineSimilarity(params.queryVector, title_vector) + 1.0",
        "params": {
          "title_vector": [1,2,3]
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![MJB](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MJB](https://discuss.elastic.co/u/MJB)\
**Post date:** [August 15, 2020, 3:28pm UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/4 "2020-08-15T15:28:05Z")

</div>

Update  
trying the approach shown here although I'd prefer to use just the Java API [https://discuss.elastic.co/t/cannot-set-up-script-score-for-dense-vector-using-java-rest-api/238849/3](https://discuss.elastic.co/t/cannot-set-up-script-score-for-dense-vector-using-java-rest-api/238849/3). This gets executed but all scores are 1.0 - only the match\_all query gets executed. What I'm doing wrong?

```
  SearchTemplateRequest request = new SearchTemplateRequest();
    request.setRequest(searchRequest);
    request.setScriptType(ScriptType.INLINE);

    def script =
            "{ \"query\": " +
                    "{" +
                    " \"script_score\": { " +
                    " \"query\": { " +
                    " \"query_string\" : { " +
                    " \"query\" : \"" + "match_all" + "\", " +
                    " : \"{}\" " +
                    " } " +
                    " }, " +
                    " \"script\": { " +
                    " \"source\": \"cosineSimilarity( " + vectors+ ",'title_vector') + 1.0\" " +
                    " } " +
                    " } " +
                    "}" +
                    "} ";
    request.setScript(script);
    SearchResponse s = elastic.search(request.getRequest(), RequestOptions.DEFAULT)
```

---

<div class="post-metadata">

**Author:** ![MJB](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MJB](https://discuss.elastic.co/u/MJB)\
**Post date:** [August 15, 2020, 8:26pm UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/5 "2020-08-15T20:26:27Z")

</div>

Duh - there is an searchTemplate option. Using it however gives error  
` [search_template] params doesn't support values of type: VALUE_NULL"}],"type":"x_content_parse_exception",`

The error persists even if I add script params in the request and it's barely mentioned on the web - what's happening?

---

<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:** [September 12, 2020, 8:26pm UTC](https://discuss.elastic.co/t/querying-dense-vector-from-the-high-level-rest-java-api/244956/6 "2020-09-12T20:26:33Z")

</div>

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