How To Employ Basic Elasticsearch Commands

Reading Time: 5 minutes

What is Elasticsearch?

Elasticsearch is a distributed, full-text, open-source search engine. It provides multi-tenant capabilities in analyzing aggregate data types from sources like Logstash or Kibana. This application stores and indexes information, which can then be queried for specific data. It returns useful details about a particular program, log analysis, application performance data, or other information. 

Installation Order

To install the Elastic Stack, deploy these applications in the following order.

  1. Elasticsearch (install instructions)
  2. Kibana (install)
  3. Logstash (install)
  4. Beats (install instructions)
  5. APM Server (install instructions)
  6. Elasticsearch Hadoop (install instructions)

Installation 

In order to install Elasticsearch, see our kb article for more in depth instructions. Here are the basic installation steps.

  1. Download and unpack the Elasticsearch official distribution.
  2. Next, run bin/elasticsearch on Linux or macOS. Run bin\elasticsearch.bat on Windows.
  3. Then, curl -X GET http://localhost:9200/.
  4. Start more servers

What is Indexing?

Indexing is simply the process of adding data into Elasticsearch. Elasticsearch stores and retrieves this data in Apache Lucene indexes. We will not be discussing Lucene specifically in this article because we need to delve deeper into that application to truly understand the role Elasticsearch plays. This article is for primarily for new users to employ basic GET and PUT requests in Lucene.

Requests

Put

We send PUT requests when we know, or want to specify an ID of the data type. We can use POST if we want Elasticsearch to generate an ID for that item on its own. An Example of a simple POST command would look like this.

curl -XPOST 'localhost:9200/logs/test_app' -H 'Content-Type: application/json' -d'
{
"timestamp": "2020-08-20 09:10:11",
"message": "Test user is logged in",
"user_id": 2,
"admin": false
}
'

And the output looks like this:

{"_index":"logs","_type":"test_app","_id":"e8rHCnQBSXUbYazxinrq","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

Output

We can see in the output provided above that id was generated by Elasticsearch., We can also note the “version” and “created” fields as well. This implies that the test_app file was created using our POST command which did not exist before. Let’s review how we can index something using a PUT request.

curl -X PUT 'localhost:9200/app/users/4' -H 'Content-Type: application/json' -d '
{
  "id": 2,
  "username": "Dean",
  "last_login": "2020-08-20 09:10:11"
}
'

Using this command, we get this output.

{"_index":"app","_type":"users","_id":"4","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

With this entry, we indexed the user Dean under /app/users/ directory. 

All the data that was entered is sent as a JSON object. If we ask how this works without any data structure, the answer is succinct. Elasticsearch usually works as a NoSQL database, thus structure is not needed. Next, we will check how we can perform queries using Elasticsearch.

What is Querying?

Querying is simply using a crafted query search to locate specific information within a given location. Using a query, we can separate questions in elasticsearch into two main categories. 

  • Leaf Queries – Leaf queries are looking for specific values in certain fields. We can run Leaf Query independently and some are termed match, term, and range queries.  
  • Compound Queries – Compound queries are combinations of leaf queries and compound queries. Compound queries combine multiple queries into a single one we use to get what we’re seeking more easily. 

Query Types

A complete classification of Elasticsearch queries looks like this:

  • Leaf Queries
    • Full text
    • Term queries
    • Geo queries
    • Span Queries
    • Joining Queries
    • Specialized queries
  • Compound Queries
    • Bool
    • Boosting
    • Dis max
    • Constant score
    • Function score

We do not have to know all these query types to run some basic commands with elasticsearch as many of these are advanced and require more than basic knowledge of the program. We list them here to get a better overall picture of elasticsearch’s capabilities. Let’s review some basic query commands that can be used with Elasticsearch. 

Note:
To query something with the Elasticsearch, we must index it first!

Examples

Since we indexed our app using the previous commands, let’s try to query it now. A simple command would look like this.

curl -XGET 'localhost:9200/app/users/4?pretty'

And this would be the output:

{
  "_index" : "app",
  "_type" : "users",
  "_id" : "4",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : 2,
    "username" : "Dean",
    "last_login" : "2020-08-20 09:10:11"
  }
}

Using this command, we queried all the users under “app”. It would return basic information such as username, id, time of last login and so on. Every field with the underscore in front of it is the meta field of our query. Under _source, we can see the original document that was indexed. The simplest way to query Elasticsearch is to use a “URI Search”. We provide a single search term or word and Elasticsearch will comb through every field of every document in our cluster. For example, we can use this query:

curl -XGET 'localhost:9200/_search?q=dean'

And Elasticsearch will find every field that contains the word “Dean” in it. On our end, the output query will look like this.

{"took":60,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.2876821,"hits":[{"_index":"app","_type":"users","_id":"4","_score":0.2876821,"_source":
{
  "id": 2,
  "username": "Dean",
  "last_login": "2020-08-20 09:10:11"
}
}]}}

Output

As we can see, the output produces both search terms, and the result of the search. Let’s do a quick breakdown of this information.

took = number of seconds that query took to complete

timed_out = pretty straightforward; false means that query didn’t time out, true means that it did time out

shards = number of documents that Elasticsearch went through and how many of them were searched successfully, how many were skipped, and how many failed

hits = how many fields actually met the query criteria, along with the meta information

In the example above, we searched for specific fields within all of our indexed information noted in Elasticsearch. We can narrow this query down further by providing the path to a limited subset of documents. This way, we can cut down on the query time as well. So, instead of: 

curl -XGET 'localhost:9200/_search?q=dean'

We will use:

curl -XGET 'localhost:9200/app/users/_search?q=dean'

The output would be the same, but in this case “took” (search timeframe) was only 3 seconds as opposed to the 60 seconds that we observed in the previous query. This is handy when we know the specific directory where some information is stored, but are not sure which document holds the information that we need. 

Now that we know the very basics of indexing and querying with Elasticsearch, let’s move on to the deletion of indexed documents.

What is Deleting?

In the same way we used previous commands, we can use the Delete function to remove data stored in our cluster. The difference is, we use DELETION via an HTTP request. 

curl -XDELETE 'localhost:9200/app/users/4?pretty'

Our output will look like this:

[root@host ~]# curl -XDELETE 'localhost:9200/app/users/4?pretty'
{
  "_index" : "app",
  "_type" : "users",
  "_id" : "4",
  "_version" : 1,
  "result" : "not_found",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

In order to delete a single index, following command can be used.

curl -XDELETE 'localhost:9200/logs?pretty'

If we want to delete an entire document that has been indexed, we can use this command.

curl -XDELETE 'localhost:9200/path/to/document'

The response that we get in both cases will look like this.

{
 "acknowledged" : true
}

Conclusion

In this article, we covered some of the basic Elasticsearch commands, but barely scratched the surface of all its capabilities. Multiple query variations can be employed to search through our information, but we would need multiple articles to cover all of them in depth. We hope that this article provides some of the basics of Elasticsearch as it is only written as an introduction to this powerful and versatile search engine.

Join Us!

Contact us today at 1.800.580.4985 to speak to a knowledgeable Solutions Provider who can get you the info you need on any one of our product lines, to assist you in making an informed decision right away.

Too busy to talk? Click HERE to open a quick chat with us to find out more. Would you like the information in an email you can review at your leisure? Email us today to get solid advice on which product in our line up would best suit your needs.

We look forward to hearing from you!

How To Install Oracle Java 8 on Ubuntu 16.04

Reading Time: 4 minutes

Oracle’s Java 8 is a programming language that creates many of the applications and websites in use today.  Its ubiquitous and stable nature makes it a favorite amongst Google and Android developers for the last 20 years. If you have a Liquid Web Core Managed Ubuntu LTS VPS server, contact our support team for help with installation.  Otherwise, this tutorial guides you through how to install Java 8 (Java Development Kit) and set the path of the JAVA_HOME variable.

Continue reading “How To Install Oracle Java 8 on Ubuntu 16.04”

How to Install OpenJDK 11 on CentOS 8

Reading Time: 5 minutes

What is OpenJDK?

openjdk logo

OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.

In this article, we will be installing OpenJDK on Centos 8.

Prerequisites

  • A Liquid Web core managed server running CentOS 8.
  • An account with root privileges
  • A root password set up on our system.
  • Access to a terminal window or an SSH command-line interface
[root@host2 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@host2 ~]#

Install OpenJDK 11

First, we want to locate the available versions of OpenJDK listed in the CentOS 8 repository using the following command.

[root@host2 ~]# dnf search jdk
Last metadata expiration check: 0:02:37 ago on Fri Aug 21 15:09:18 2020.
========== Name & Summary Matched: jdk ==========
java-11-openjdk-demo.x86_64 : OpenJDK Demos 11
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.x86_64 : OpenJDK Source Bundle 11
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle 8
java-11-openjdk.x86_64 : OpenJDK Runtime Environment 11
copy-jdk-configs.noarch : JDKs configuration files copier
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment 8
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
>>>java-11-openjdk-devel.x86_64 : OpenJDK Development Environment 11
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-11-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 11
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK 8 accessibility connector
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 8
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in single archive
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed in single archive
========== Summary Matched: jdk ==========
jmc-core.noarch : Core API for JDK Mission Control
jmc.x86_64 : JDK Mission Control is a profiling and diagnostics tool
icedtea-web.noarch : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation
[root@host2 ~]#  
Note:
While we can run Java apps directly using the JRE, our apps will have to be compiled each time they are executed, which tends to be slower than running apps that have already been compiled. This would not be desirable if we plan to execute apps multiple times.

As you can see above, the version we want to install is:
java-11-openjdk-devel.x86_64 : OpenJDK Development Environment 11

To install this version, run the following command.

[root@host2 ~]# dnf install java-11-openjdk-devel.x86_64
Last metadata expiration check: 0:09:28 ago on Fri Aug 21 15:19:11 2020.
Dependencies resolved.
========================================================================
 Package Architecture Version Repository Size
========================================================================
Installing:
 Java-11-openjdk-devel x86_64 1:11.0.8.10-0.el8_2 system-AppStream 3.4 M

Installing dependencies:
…
…
…
...
Enabling module streams:
 javapackages-runtime 201801

Transaction Summary
========================================================================
Install 63 Packages

Total download size: 68 M
Installed size: 246 M
Is this ok [y/N]:   
Downloading Packages:
(1/63): cups-libs-2.2.6-33.el8.x86_64.rpm 15 MB/s | 432 kB 00:00
(2/63): glib-networking-2.56.1-1.1.el8.x86_64.rpm 9.9 MB/s | 155 kB 00:00
(3/63): gdk-pixbuf2-2.36.12-5.el8.x86_64.rpm 8.9 MB/s | 467 kB 00:00
…
…
…
------------------------------------------------------------------------------------------------------------------------
Total 73 MB/s | 68 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Running scriptlet: copy-jdk-configs-3.7-1.el8.noarch 1/1
  Running scriptlet: java-11-openjdk-headless-1:11.0.8.10-0.el8_2.x86_64 1/1
…
…
…
Verifying : tzdata-java-2020a-1.el8.noarch 61/63
Verifying : xorg-x11-font-utils-1:7.5-40.el8.x86_64 62/63
Verifying : xorg-x11-fonts-Type1-7.5-19.el8.noarch 63/63
Installed:
java-11-openjdk-1:11.0.8.10-0.el8_2.x86_64 java-11-openjdk-devel-1:11.0.8.10-0.el8_2.x86_64
java-11-openjdk-headless-1:11.0.8.10-0.el8_2.x86_64 javapackages-filesystem-5.3.0-1.module_el8.0.0+11+5b8c10bd.noarch

Complete!
[root@host2 ~]#

Verify Installation

Once the installation is completed, you can verify the installed Java version by running the following command.

[root@host2 ~]# java -version
openjdk version "11.0.8" 2020-07-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.8+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode, sharing)
[root@host2 ~]# 

Since we have chosen the full installation of the OpenJDK development kit, we can check the compiler version as well.

[root@host2 ~]# javac -version
javac 11.0.8
[root@host2 ~]#

Install OpenJDK 8

Some Java apps only run on OpenJDK 8. If this is the case, we need to install OpenJDK 8. We can install OpenJDK 8 using the following command.

[root@host2 ~]# dnf install java-1.8.0-openjdk-devel.x86_64

Verify Installation

After installing this version of Java, we can check the version using the following command.

[root@host2 ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b09)
OpenJDK 64-Bit Server VM (build 25.242-b09, mixed mode)
[root@host2 ~]#

Set Java Environment Variables

Now, we need to set the environmental variables for the new Java installations. This allows us to add both the JAVA_HOME and PATH settings, which will ensure that our Java applications can run without issue.

cat > /etc/profile.d/java11.sh <<EOF
export JAVA_HOME=\$(dirname \$(dirname \$(readlink \$(readlink \$(which javac)))))
export PATH=\$PATH:\$JAVA_HOME/bin
export CLASSPATH=.:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib:\$JAVA_HOME/lib/tools.jar
EOF

Next, we will source the file to begin using it without having to log out and log back in.

[root@host2 ~]# source /etc/profile.d/java11.sh

To confirm the changes, run the following commands.

[root@host2 ~]# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el8_2.x86_64

[root@host2 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64/bin:/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el8_2.x86_64/bin:/bin

If after installing both versions of Java by some chance show the following variables:

[root@host2 ~]# echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64

[root@host2 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64/bin

and we wish to change the JAVA_HOME and PATH variables back to Java 11, run the following commands.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el8_2.x86_64
export PATH=$PATH:$JRE_HOME/bin

We can also view the following folder location to see the full view of all the Java installations.

[root@host2 ~]# ll /usr/lib/jvm/
total 8
lrwxrwxrwx 1 root root 26 Aug 21 17:23 java -> /etc/alternatives/java_sdk
lrwxrwxrwx 1 root root 32 Aug 21 16:06 java-1.8.0 -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx 1 root root 40 Aug 21 16:06 java-1.8.0-openjdk -> /etc/alternatives/java_sdk_1.8.0_openjdk
drwxr-xr-x 7 root root 4096 Aug 21 16:06 java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64
lrwxrwxrwx 1 root root 29 Aug 21 15:49 java-11 -> /etc/alternatives/java_sdk_11
lrwxrwxrwx 1 root root 37 Aug 21 15:49 java-11-openjdk -> /etc/alternatives/java_sdk_11_openjdk
drwxr-xr-x 7 root root 4096 Aug 21 15:49 java-11-openjdk-11.0.8.10-0.el8_2.x86_64
lrwxrwxrwx 1 root root 34 Aug 21 16:06 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx 1 root root 21 Aug 21 18:04 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root 27 Aug 21 16:06 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx 1 root root 35 Aug 21 16:06 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
lrwxrwxrwx 1 root root 51 Jul 16 11:10 jre-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64 -> java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64/jre
lrwxrwxrwx 1 root root 24 Aug 21 15:49 jre-11 -> /etc/alternatives/jre_11
lrwxrwxrwx 1 root root 32 Aug 21 15:49 jre-11-openjdk -> /etc/alternatives/jre_11_openjdk
lrwxrwxrwx 1 root root 40 Jul 16 11:06 jre-11-openjdk-11.0.8.10-0.el8_2.x86_64 -> java-11-openjdk-11.0.8.10-0.el8_2.x86_64
lrwxrwxrwx 1 root root 29 Aug 21 16:06 jre-openjdk -> /etc/alternatives/jre_openjdk
[root@host2 ~]# 

Change Java Versions

If we have several versions of Java installed, we can select an alternative if need be. To change versions, run the following command. 

[root@host2 ~]# alternatives --config java

There are 2 programs which provide 'java'.

  Selection Command
-----------------------------------------------
 + 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el8_2.x86_64/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64/jre/bin/java)

Enter to keep the current selection[+], or type selection number: 1
[root@host2 ~]#   

In this case, we selected number 1, to keep the java-11-openjdk.x86_64 version. 

Conclusion

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our talented Support Teams are full of experienced Linux technicians and System administrators who have years of experience in web hosting technologies, especially those discussed in this article. We are always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone @800.580.4985, a chat or support ticket to assisting you with this process.

How to Install and Configure Elasticsearch

Reading Time: 8 minutes

What is Elasticsearch?

elasticsearch logo

Elasticsearch is a distributed, open-source, full-text search engine which provides multi-tenant capabilities for analyzing multiple data types. It stores and indexes data shipped from sources like Logstash or Kibana. Elasticsearch can then be queried for specific data to return useful information about a particular application, log analysis, application performance data, or other information. 

Continue reading “How to Install and Configure Elasticsearch”

How to Install Wildfly on CentOS 8

Reading Time: 4 minutes

What is Wildfly?

WildFly is a Java runtime application server and software management platform implemented in Java. It is primarily used to provision Java applications and services on a Java-based platform. It is currently developed by Red Hat as open-source software (apart from the community forums) and was better-known as JBoss AS previously. While the WildFly software remains open-source, paid support can be purchased from RedHat if needed. RedHat continues to implement a separate version of this software under the JBoss EAP name.

Continue reading “How to Install Wildfly on CentOS 8”

How to Install Java on Ubuntu, Windows, and MacOS

Reading Time: 6 minutes

What is Java?

Java is considered one of the most popular programming languages. Java is a typed object-oriented programming language developed by Sun Microsystems (and subsequently acquired by Oracle). Development is mainly performed by a community organized through the Java Community Process. The language and the main technologies that implements it are distributed under the GPL but the trademark rights are owned by Oracle Corporation. Code written in Java (using the JVM – Java Virtual Machine) is transformed into bytecode and due to this, programs can run on any system where the JVM is installed. Therefore, Java is often called a cross-platform language. Most often, Java is used to develop backend programs and android applications.

Continue reading “How to Install Java on Ubuntu, Windows, and MacOS”

How To Install WildFly On CentOS 7

Reading Time: 5 minutes

Introduction

In this tutorial, our objective is to install WildFly on an unmanaged CentOS 7 server. WildFly is a managed application runtime server and Java software platform used to provision Java applications and services. It is an open-source software which is consistently being improved by its user community. While the WildFly software remains open-source, if paid support is needed, it is provided by Red Hat who currently develops the software (apart from the community forums). Some may be more familiar with WildFly’s previous name, which was JBoss AS. RedHat continues to develop its version of this software under the JBoss EAP name.

Continue reading “How To Install WildFly On CentOS 7”