# Java Client Library - ELSER

**URL:** <https://discuss.elastic.co/t/java-client-library-elser/355905>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning, language-clients\
**Created:** [March 21, 2024, 1:22pm UTC](https://discuss.elastic.co/t/java-client-library-elser/355905 "2024-03-21T13:22:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sivagurlinka](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sivagurlinka](https://discuss.elastic.co/u/sivagurlinka)\
**Post date:** [March 21, 2024, 1:22pm UTC](https://discuss.elastic.co/t/java-client-library-elser/355905/1 "2024-03-21T13:22:17Z")

</div>

We are trying to leverage ELSER search functionalities within our Java application. we would like to know **How to use Java client library for performing ELSER Search ?**

RestHighLevelClient seems to be one solution we observed. But that is deprecated. Is there any examples or approaches to use ELSER search using Client library, similar to [Searching for documents | Elasticsearch Java API Client [8.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/searching.html)

#Elasticsearch #java #language-clients #elastic-stack-machine-learning

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 21, 2024, 2:57pm UTC](https://discuss.elastic.co/t/java-client-library-elser/355905/2 "2024-03-21T14:57:10Z")

</div>

What is the API you'd like to use?

If you want to use something like:

```auto
# Create Index
PUT my-index
{
  "mappings": {
    "properties": {
      "content_embedding": { 
        "type": "sparse_vector" 
      },
      "content": { 
        "type": "text" 
      }
    }
  }
}
# Create Pipeline
PUT _ingest/pipeline/elser-v2-test
{
  "processors": [
    {
      "inference": {
        "model_id": ".elser_model_2",
        "input_output": [ 
          {
            "input_field": "content",
            "output_field": "content_embedding"
          }
        ]
      }
    }
  ]
}
# Search
GET my-index/_search
{
   "query":{
      "text_expansion":{
         "content_embedding":{
            "model_id":".elser_model_2",
            "model_text":"How to avoid muscle soreness after running?"
         }
      }
   }
}

```

Then it would be something like (with 8.12.2 - **not tested** ):

```auto
// Create the index
client.indices().create(cir -> cir.index(indexName).mappings(m -> m
        .properties("content", p -> p.text(tp -> tp))
        .properties("content_embedding", p -> p.sparseVector(sp -> sp))
));

// Create the pipeline
client.ingest().putPipeline(pr -> pr
        .id("elser-v2-test")
        .processors(p -> p
                .inference(i -> i
                        .modelId(".elser_model_2")
                        .fieldMap("content", JsonData.of("content"))
                        .targetField("content_embedding")
                )
        )
);

// Search
client.search(sr -> sr
        .index(indexName)
        .query(q -> q.textExpansion(te -> te
                .field("content_embedding")
                .modelId(".elser_model_2")
                .modelText("How to avoid muscle soreness after running?")
        )), ObjectNode.class);

```

---

<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:** [April 18, 2024, 2:57pm UTC](https://discuss.elastic.co/t/java-client-library-elser/355905/3 "2024-04-18T14:57:44Z")

</div>

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