1

Currently I'm using SQL queries in Elasticsearch but wanted to convert them to Elasticsearch DSL due to some data issues.

My current sql query is

{
    "query": "select \"A\",\"B\" from \"sometable\"  ORDER BY datetime DESC LIMIT 10"
}  

which returns these results:

{
    "columns": [
        {
            "name": "A",
            "type": "float"
        },
        {
            "name": "B",
            "type": "float"
        }
    ],
    "rows": [
        [
            81.22833251953125,
            22.495885848999023
        ],
        [
            81.22833251953125,
            22.495885848999023
        ]
    ]
}

But I want the same result using Elasticsearch DSL in this format [[x,y],[x,y]]. I have also tried to translate the same SQL query to Elasticsearch DSL but it returns a different result.

The DSL query I have created so far is below but how we can limit the records for example 100.

{
   "aggs": {
        "documentCounts": {
          "scripted_metric": {
            "init_script": "state.transactions = []",
            "map_script": "state.transactions.add( [doc['A'].value,doc['B'].value] )",
            "combine_script": " return state.transactions"
          }
       }
    }
}

1 Answer 1

1

You could use Apache Calcite which provides an ElasticSearch adapter.

In the documentation you have an example on how to use the CSV adapter: https://calcite.apache.org/docs/tutorial.html, you can easily change the setup files to achieve the same for ES.

At that point you can use sqlline to query ES via SQL statement, Calcite will do the translation to ES QL, and fetch the results.

In order to print the query that is sent to ES, simply change the log level to DEBUG for the class ElasticsearchTable, for instance using this log4j.properties file:

# Set root logger level to DEBUG and its only appender to A1.
log4j.category.org.apache.calcite.adapter.elasticsearch.ElasticsearchTable=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Then you can point to this log4j property file in sqlline script as follows:

export JAVA_OPTS="-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Dlog4j.configuration=file://$YOUR_PATH/log4j.properties"

Now simply run your select statement via sqlline and check for the debug string starting with Elasticsearch Query: in the console.

Here an example of what I get as output:

$ ./sqlline
Building Apache Calcite 1.30.0-SNAPSHOT
sqlline version 1.12.0
sqlline> !connect jdbc:calcite:model=model.json admin admin
log4j:WARN No appenders could be found for logger (org.apache.calcite.adapter.elasticsearch.ElasticsearchSchemaFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Transaction isolation level TRANSACTION_REPEATABLE_READ is not supported. Default (TRANSACTION_NONE) will be used instead.
0: jdbc:calcite:model=model.json> select * from "zips";
3576 [main] DEBUG org.apache.calcite.adapter.elasticsearch.ElasticsearchTable  - Elasticsearch Query: {"size":5196}
+----------------------------------------------------------------------------------+
|                                       _MAP                                       |
+----------------------------------------------------------------------------------+
| {id=01701, city=FRAMINGHAM, loc=[-71.425486, 42.300665], pop=65046, state=MA}    |
| {id=02154, city=NORTH WALTHAM, loc=[-71.236497, 42.382492], pop=57871, state=MA} |
+----------------------------------------------------------------------------------+
2 rows selected (0.974 seconds)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the reply Alessandro, I will check this.
Hi Alessandro, I have set up Apache Calcite and run the query, It shows the required results but where can I see the translated SQL query?
Did you run it programmatically or via sqline? If it's the former, can you share your code snippet somewhere?
I have run it using sqline.
Hi Alessandro Thanks for the comment. I will try it and let you know. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.