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

[Question] Is there a way to convert ES queries to bodybuilder implementation? #294

Open
lucianopf opened this issue Oct 20, 2021 · 3 comments

Comments

@lucianopf
Copy link

I've been trying to convert complex ES queries to bodybuilder but it's quite hard to figure it out without much knowledge of the library.
Maybe it would be nice to have a converter from query to bodybuilder to help newcomers to learn how does the lib works 😬

@danpaz
Copy link
Owner

danpaz commented Oct 21, 2021

I've thought for a while that something like this would be useful, but I think implementing it would be pretty difficult since the api is so flexible.

Have you tried using https://bodybuilder.js.org to try to recreate your queries using bodybuilder? You can also ask here if you have a specific complex query, I or someone else may be able to help you along.

@tx0c
Copy link

tx0c commented Apr 27, 2022

reverse convert is so hard, is @danpaz able to convert this back to bodybuilder js code ?

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "content.keyword": {
                  "value": "小說",
                  "boost": 2
                }
              }
            },
            {
              "match": {
                "content": "小說"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "numAuthors",
            "factor": 1.2,
            "modifier": "log2p",
            "missing": 1
          }
        },
        {
          "field_value_factor": {
            "field": "numArticles",
            "modifier": "log2p",
            "missing": 1
          }
        }
      ]
    }
  },
  "size": 20
}

@danpaz
Copy link
Owner

danpaz commented May 4, 2022

@tx0c the function_score query could be expressed in bodybuilder syntax pretty easily, but the functions array is probably best handled separately. Give this a try:

let body = bodybuilder()
  .query('function_score', b => {
    return b
      .orQuery('term', 'content.keyword', { value: '小說', boost: 2 })
      .orQuery('match', 'content', '小說')
  })
  .build()

body.query.function_score.functions = [
  {
    "field_value_factor": {
      "field": "numAuthors",
      "factor": 1.2,
      "modifier": "log2p",
      "missing": 1
    }
  },
  {
    "field_value_factor": {
      "field": "numArticles",
      "modifier": "log2p",
      "missing": 1
    }
  }
]

return body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants