elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Java API Client 8.x SettingsSimilarity doesnt have field 'type' for CreateIndexRequest API

Open notrickyd opened this issue 3 years ago • 1 comments

Java API client version

8.3.3

Java version

17

Elasticsearch Version

8.3.3

Problem description

 new CreateIndexRequest.Builder().
        .settings(s -> s
                .similarity(
                    new SettingsSimilarity.Builder().lmj(
                        new SettingsSimilarityLmj.Builder()
                            .lambda(2)
                            .build()
                    ).build()
                )

Response from Elasticsearch is: [es/indices.create] failed: [illegal_argument_exception] Similarity [lmj] must have an associated type

This is the same error received from Elasticsearch when you run REST api with JSON:

PUT /index
{
  "settings": {
     "similarity": {
        "lmj": {
           "lambda": 2
        }
      }
   }  
}

And there is no possibility to write custom similarities or specify wanted field type in existing ones. As well as you cannot specify similarity-name with this Java API.

This issue starts from 8.0.1 till 8.3.3 (current). In earlier versions there are no SimilarityXXX classes at all.

notrickyd avatar Aug 05 '22 16:08 notrickyd

Any reaction here?

notrickyd avatar Aug 16 '22 23:08 notrickyd

This just hit me as well, there doesn't seem to be a way to configure a custom similarity configuration. In the resulting JSON, lmj is the name, and the value lmj should actually be in the type attribute of the object.

I'm working around this by using otherSettings on the IndexSettings, but that's painful.

EgbertW avatar Dec 14 '22 07:12 EgbertW

Hello, thank you for the report! This was an issue with the API specification used to produce the Java code, it has been just fixed and will be part of the next release. Here is an example of how configure a similarity in a new index:

        client.indices().create(cr -> cr
            .index("new")
            .settings(st -> st
                .index(in -> in
                    .similarity("customBM25",sim -> sim
                        .bm25(bm25 -> bm25
                            .discountOverlaps(true)
                            .b(1.0)
                            .k1(0.1))
                    )
                )
            )
        );

and then how to reference it in the mapping:

        client.indices().putMapping(pm -> pm
            .index("new")
            .properties("title",p -> p
                .text(tx -> tx
                    .similarity("customBM25"))));

Thank you for you patience :)

l-trotta avatar Mar 18 '24 14:03 l-trotta