Giter VIP home page Giter VIP logo

Comments (7)

flobernd avatar flobernd commented on June 11, 2024 1

Hi @insulationman,

thanks for requesting this feature. The decay functions will be implemented soon. They require some special handling to get generated correctly.

from elasticsearch-net.

flobernd avatar flobernd commented on June 11, 2024 1

Hi @jasielmacedo,

but intercepting the JSON or executing a complete JSON request? why use this library anyway?

This library supports more than 450 endpoints and nearly 3000 types. I spent the past 6+ months continuing the good work of Martijn and Steve to get the code generator to a point where it finally supports 95%+ of the overall available Elasticsearch functionality. For me, these are at least 450 reasons to use the library, even if I would have to manually craft one JSON request myself - but that's of course just my personal opinion.

I think suggesting to not use this library with a product that was developed using this version 8 of the library is insane.

I think we misunderstood here. I'm strongly suggesting to use the library! You can use the regular client for everything besides the query that uses decay functions. For this particular case, you can fall-back to the low-level client (which still saves you from having to build the complete transport level layer yourself). The low level client is exposed by the regular client:
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/migration-guide.html#_workarounds_for_missing_features

I know that the above workaround is not perfect. At the same time, please note, that a lot has been added to the library in the last weeks and a lot of bugs have been fixed and usability problems have been addressed. I'm sorry, that the decay functions were not part of this yet, but they are on my todo list and I excpect then to be available within weeks.

What I'm asking is just a possibility of creating my own FunctionScore so I don't need to rewrite the whole code just to add one additional feature

I can give you another hacky alternative. Using reflection you could access the internal constructor of FunctionScore to manually create an instance with a custom variant:

internal FunctionScore(string variantName, object variant)

For variantName use one of linear, gauss or exp.

For variant, create your own typed function score class or use JSON for this part like this:

var gauss = """
{
  "timestamp": {
    "scale": "4w"
  }
}
"""

var variant = new RawJsonString(gauss);

The RawJsonString class is a special struct that serializes the contained string as raw json (without quotes).

from elasticsearch-net.

jasielmacedo avatar jasielmacedo commented on June 11, 2024 1

@flobernd Thanks. Yeah, I was wrong. It wasn't clear in the beginning but reading the code of the library I figured out.
I ended up creating a similar solution using the Transport class you suggested, let me share here so if someone has the same issue they could adapt for their case as well while the final solution is not ready:

For the request I just updated the way I execute the query. I just created a delegate so I can intercept the json.

var json = _client.RequestResponseSerializer.SerializeToString(searchRequestDescriptor);
json = AddGaussFunction(json);

var queryParameters = new DefaultRequestParameters();
// I added this because I'm using aggregations and suggester
queryParameters.SetQueryString("typed_keys", true);

var response = await _client.Transport.RequestAsync<SearchResponse<T>>(Elastic.Transport.HttpMethod.POST, $"/{indexName}/_search", PostData.String(json), queryParameters).ConfigureAwait(false);
if (!response.IsValidResponse)
{
    /// throw error or do whatever you want to do with the error
}

return response;

So My case the function score was being added right after the first query so it was simple to update the json, but with a simple tweak it can be used anywhere

void AddGaussFunction(string jsonString, int scale, int offset)
{
    try
    {
        JsonObject jsonObject = JsonNode.Parse(jsonString).AsObject();
        if (jsonObject["query"]?["function_score"]?["functions"] is JsonArray functionsArray)
        {
            var now = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();            
            var gaussFunction = JsonNode.Parse($@"
                {{
                    ""gauss"": {{
                        """": {{
                            ""origin"": ""{now}"",
                            ""scale"": ""{scale}"",
                            ""offset"": ""{offset}"",
                            ""decay"": 0.7
                        }}
                    }},
                    ""weight"": 1.2,
                    ""filter"": {{
                        ""term"": {{
                            ""customfield"": ""CustomType""
                        }}
                    }}
                }}
            ").AsObject();

            functionsArray.Add(gaussFunction);
            return jsonObject.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
        }
        else
        {
            return jsonString;
        }
    }
    catch (JsonException e)
    {
        throw new InvalidOperationException("Error parsing JSON.", e);
    }
}

from elasticsearch-net.

jasielmacedo avatar jasielmacedo commented on June 11, 2024

@flobernd So I think it's better to at least give us options to create our own solution for that. Like I'm stuck trying to think in a workaround to use gauss function and even the Serializer function you put internal ctor So I can't create my own descriptor for example locally

It's been a year #7428

You don't have time to create this function but you don't allow anyone else to at least try to create their own solution for that locally? This version 8 is incomplete and you have to give devs options to keep using it.

from elasticsearch-net.

flobernd avatar flobernd commented on June 11, 2024

From the other post:

you could consider dropping to the Transport layer (available from the client instance), where you can send a complete request as JSON and handle the JSON response

An example of how to use the low level transport can be found in the migration guide.

from elasticsearch-net.

jasielmacedo avatar jasielmacedo commented on June 11, 2024

@flobernd thanks for the suggestion but intercepting the JSON or executing a complete JSON request? why use this library anyway?
I appreciate you came here to answer this, but I think suggesting to not use this library with a product that was developed using this version 8 of the library is insane.

What I'm asking is just a possibility of creating my own FunctionScore so I don't need to rewrite the whole code just to add one additional feature

from elasticsearch-net.

flobernd avatar flobernd commented on June 11, 2024

Related to: elastic/elasticsearch-specification#2545

from elasticsearch-net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.