CentOS Stream 8
Sponsored Link

Elastic Stack 7 : Install Elasticsearch
2021/06/22
 
Install Full-Text search engine [Elasticsearch].
[1] Install and Run Elasticsearch.
Installing Java is not required anymore becasue integrated Java is included in Elasticsearch.
[root@dlp ~]#
cat > /etc/yum.repos.d/elasticsearch.repo <<EOF
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
[root@dlp ~]#
dnf -y install elasticsearch
[root@dlp ~]#
systemctl enable --now elasticsearch
# verify status

[root@dlp ~]#
curl http://127.0.0.1:9200

{
  "name" : "dlp.srv.world",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "7qvvnLl8SueX33dp5HaGHA",
  "version" : {
    "number" : "7.13.2",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "4d960a0733be83dd2543ca018aa4ddc42e956800",
    "build_date" : "2021-06-10T21:01:55.251515791Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
[2]
If you use Elasticsearch from other Hosts, refer to the setting for Clustering.
It needs to configure the same settings with Clustering even if single node using if receiving requests from other Hosts.
[3] This is the basic usage of Elasticsearch.
Create an Index first, it is like Database on RDB.
# show Index list ([pretty] means it shows JSON with human readable)

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{ }
# create Index

[root@dlp ~]#
curl -X PUT "http://127.0.0.1:9200/test_index"

{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}
# verify

[root@dlp ~]#
curl http://127.0.0.1:9200/_aliases?pretty

{
  "test_index" : {
    "aliases" : { }
  }
}
[root@dlp ~]#
curl http://127.0.0.1:9200/test_index/_settings?pretty

{
  "test_index" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test_index",
        "creation_date" : "1624321329152",
        "number_of_replicas" : "1",
        "uuid" : "Vh0HutpLRciaMWX3pFo7Zg",
        "version" : {
          "created" : "7130299"
        }
      }
    }
  }
}
[4] Define Mapping and insert test data.
Mapping defines structure of Index. If inserting data, Mapping will be defined automatically, but it's possible to define manually, of course.
# insert data

[root@dlp ~]#
curl -H "Content-Type: application/json" \
-X PUT "http://127.0.0.1:9200/test_index/doc01/1" -d '{
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
}'
{"_index":"test_index","_type":"doc01","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

# show Mapping

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/_mapping/?pretty"

{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "subject" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

# show data

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/1?pretty"

{
  "_index" : "test_index",
  "_type" : "doc01",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
  }
}

# search data

# example of Search conditions below means [description] field includes a word [initial]

[root@dlp ~]#
curl "http://127.0.0.1:9200/test_index/doc01/_search?q=description:initial&pretty=true"

{
  "took" : 114,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "doc01",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "subject" : "Test Post No.1",
          "description" : "This is the initial post",
          "content" : "This is the test message for using Elasticsearch."
        }
      }
    ]
  }
}
Matched Content