Author Archives: Raghavendar T S

Elasticsearch – Date/Time(String) Add/Subtract Duration – Days,Months,Years,Hours,Minutes,Seconds

Elasticsearch - DateTime(String) Add/Subtract Duration - Days,Months,Years,Hours,Minutes,Seconds

Elasticsearch – Date/Time(String) Add/Subtract Duration – Days,Months,Years,Hours,Minutes,Seconds

Sample Query :

POST /test/test/_search
{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [{
                        "range": {
                            "dateOrTimeAttribute": {
                                "from": "16:31:15",
                                "to": "16:31:15||+10m"
                            }
                        }
                    }]
                }
            }
        }
    }
}

Examples:

  • “16:31:15||+10m”
  • “16:31:15||+1d”
  • “2016-02-02||-1M”

 

Java – Read/Parse Command Line Arguments/Options

Java - Read/Parse Command Line Arguments/Options

Java – Read/Parse Command Line Arguments/Options

Maven Dependency :

<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.2</version>
</dependency>

Sample Java Program :

package test;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class Test
{
    public static void main(String[] args) {

        //Define Options
        Options options = new Options();

        //Option Name - option1
        //Has input value - true
        options.addOption("option1", true, "Option 1 Description");

        //Option Name - option2
        //Has input value - false
        options.addOption("option2", false, "Option 2 Description");

        CommandLineParser parser = new BasicParser();
        CommandLine cmd = null;

        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        System.out.println(cmd.hasOption("option1"));
        System.out.println(cmd.hasOption("option2"));

        System.out.println(cmd.getOptionValue("option1"));
        System.out.println(cmd.getOptionValue("option2"));

    }
}

Output :

Consider we execute the sample Java program with following arguments

java Test -option1 option1Value -option2

true
true
option1Value
null

option2 value is null because it does not takes input values which is defined using the Options class.

Java JConsole – Local Process List Empty – Solved

JAVA JCONSOLE – LOCAL PROCESS LIST EMPTY – SOLVED.png

Java JConsole – Local Process List Empty – Solved

JConsole or  Java Monitoring and Management Console

  1. Open command prompt
  2. Execute the command echo %TMP%
  3. Now you will get a path to a folder where temporary files are stored. E.g. C:\Users\Username\AppData\Local\Temp
  4. Go to that folder
  5. Remove the folder named hsperfdata_USERNAME
  6. Restart JConsole application and now you will have the list of currently running Java processes in the local process list
  7. You can also the JConsole directly from command line using the command  jconsole PROCESS_ID
  8. Enjoy !!!

Elasticsearch – Compare/ScriptFilter/Condition on Two Fields using Script Filter – REST Query + Java API

Elasticsearch - Compare or Script Filter or Condition on Two Fields using Script Filter - REST Query + Java API

Elasticsearch – Compare/ScriptFilter/Condition on Two Fields using Script Filter – REST Query + Java API

Example 1:

GET /INDEX/TYPE/_search
{
     "query": {
          "filtered": {
               "filter": {
                    "script": {
                         "script": "doc['field1'].value <  doc['field2'].value"
                    }
               }
          }
     }
}

Example 2:

GET /INDEX/TYPE/_search
{
     "query": {
          "filtered": {
               "filter": {
                    "script": {
                         "script": "doc['field1'].value -  doc['field2'].value > 10"
                    }
               }
          }
     }
}

Java API:

import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.FilteredQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

FilteredQueryBuilder query = QueryBuilders.filteredQuery(null, FilterBuilders.scriptFilter("doc['field1'].value <  doc['field1'].value"));
SearchRequestBuilder searchRequestBuilder = esClient.prepareSearch("INDEX").setTypes("TYPE").setQuery(query).setSize(10);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

Related Links: 

Web Application for Elasticsearch :
  1. ElasticTab – Elasticsearch to Excel Report (Web Application)
Elasticsearch Plugin:
  1. Elasticsearch Plugin To Generate (Save and E-Mail) Excel Reports
Elasticsearch:
  1. Execute Multiple Search Query in Elasticsearch
  2. Monitor Elasticsearch Servers with Shell Script - E-Mail Notification
  3. Execute Raw Elasticsearch Query using Transport Client – Java API
  4. Elasticsearch – Apply Nested Filter on Nested (Inner) Aggregation
  5. Execute Multiple Search Query in Elasticsearch
  6. Enable CORS to Send Cross Domain Request to Elasticsearch using AJAX
  7. Elasticsearch Java API – Get Index List
  8. Elasticsearch Java API – Get Alias List
  9. Elasticsearch Java API - Get Type List from given Index
  10. Elasticsearch Java API – Get Field List for a given Index and Type
  11. Elasticsearch Java API – Get Index Type List Mapping
  12. Elasticsearch – Use Script Filter/Conditon in Aggregation/Sub-Aggreagtion
  13. Elasticsearch – Compare/ScriptFilter/Condition on Two Fields using Script Filter – REST Query + Java API
  14. Elasticsearch - Date/Time(String)  Add/Subtract Duration - Days,Months,Years,Hours,Minutes,Seconds
Logstash:
  1. Logstash – Process Log File Once and Exit/Stop Logstash After Reading Log File Once
  2. Measure Logstash Performance using Metrics Filter – Issue/Error in Syntax (Unknown setting ‘message’ for stdout)
  3. Logstash – Process Same Log File (File Input) from Beginning/Start
  4. Create Custom Filter/Plugin to Emit New Events Manually in Logstash
Logstash and Elasticsearch:
  1. Query Elasticsearch Cluster in Filter Section when using Logstash
  2. Custom Elasticsearch Index Name/Type Name based on Events in Logstash
MongoDB and Elasticsearch:
  1. Import Data from Mongo DB to Elasticsearch using Elasticsearch River
 Read More...