Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow expressions to compute keys #56

Closed
larsga opened this issue Jan 30, 2019 · 7 comments
Closed

Allow expressions to compute keys #56

larsga opened this issue Jan 30, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@larsga
Copy link
Contributor

larsga commented Jan 30, 2019

Issue #55 demonstrates that there might be a need for being able to write objects like this:

{
  .foo : .bar,
  "something" : .else
}

There doesn't seem to be any clear reason why this couldn't be supported.

@larsga larsga added the enhancement New feature or request label Jan 30, 2019
@ngsoftwaredev
Copy link
Contributor

That feature would help me so much!

@larsga
Copy link
Contributor Author

larsga commented May 27, 2019

Could you show an example of how you would use it? That would be helpful for me.

@ngsoftwaredev
Copy link
Contributor

ngsoftwaredev commented May 29, 2019

Sure!

One use case I have is to generate Elasticsearch queries from a business-specific DSL, using JSLT, which allows me for far more sophisticated templating than Elasticsearch native templating (based on mustache.js)!

Here's a sample JSLT :

let searchFields = [ "title", "author", "category" ]
let facetFields = [ "category", "thema" ]
let searchText = .text

{
  "query": {
    "dis_max": {
      "tie_breaker": 1,
      "queries": [ for ($searchFields)
        {
          "fuzzy": {
            . + ".lang"  : {
              "value": $searchText,
              "fuzziness" :     2,
              "prefix_length" : 1,
              "max_expansions": 100
            }
          }
        }        
      ]
    }
  },
  "highlight": {
    "fields": { for ($searchFields)
      . + ".lang": { "number_of_fragments": 1 }
    }
  },
  "aggs": {
    for ($facetFields) {
      let field = .
      $field : {
        "terms": {
          "field": $field,
          "size": 20
        }
      }
    }
  }
}

This does not compile. This is only a very simple query, it could be much more complex, but basically I always have to use a field name as a key to specifiy some operation on it, be it a search operation, a highlight, an aggregation.

Currently I have to build a JSON string instead and the use the from-json function.

@larsga
Copy link
Contributor Author

larsga commented May 29, 2019

This makes sense. Thank you! Yes, I think we should add support for this. I'll see if I can get it done.

@larsga
Copy link
Contributor Author

larsga commented Jul 29, 2020

Now I find I need this feature myself.

@larsga
Copy link
Contributor Author

larsga commented Jul 30, 2020

Some issues that need to be worked out.

Dynamic keys in objects that use the object matcher. Since we don't know at compile-time what keys are defined in the matcher the matching behaviour becomes more complex. Possible solution: forbid dynamic keys in matchers.

What if the key expression produces something that's not a string? We consider this an error.

What if the key expression ends up producing a duplicate key? Duplicate literal keys are forbidden at the moment. Consider this an error?

@larsga
Copy link
Contributor Author

larsga commented Aug 13, 2020

Fixed in commit 7342c95

@larsga larsga closed this as completed Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants