FreeBSD 14
Sponsored Link

Elastic Stack 8 : Install Elasticsearch2024/09/25

 

Install Full-Text search engine [Elasticsearch].

[1]

Install OpenJDK 17, refer to here.

[2] Install and Run Elasticsearch.
root@dlp:~ #
pkg install -y elasticsearch8 curl
root@dlp:~ #
sysctl security.bsd.unprivileged_mlock=1

root@dlp:~ #
echo 'security.bsd.unprivileged_mlock=1' >> /etc/sysctl.conf
root@dlp:~ #
service elasticsearch enable

elasticsearch enabled in /etc/rc.conf
root@dlp:~ #
service elasticsearch start

Starting elasticsearch.
# reset admin user password

root@dlp:~ #
elasticsearch-reset-password --auto --username elastic

This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y


Password for the [elastic] user successfully reset.
New value: VC3wulb8T4euiA+DxIbO

# verify status
# password is the one that is shown above

root@dlp:~ #
curl -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200

Enter host password for user 'elastic':
{
  "name" : "dlp.srv.world",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "Tkls9_WiRm-XricowHjEVA",
  "version" : {
    "number" : "8.11.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "64cf052f3b56b1fd4449f5454cb88aca7e739d9a",
    "build_date" : "2023-12-08T11:33:53.634979452Z",
    "build_snapshot" : false,
    "lucene_version" : "9.8.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}
[3]

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.

[4] 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 -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/_aliases?pretty

Enter host password for user 'elastic':
{
  ".security-7" : {
    "aliases" : {
      ".security" : {
        "is_hidden" : true
      }
    }
  }
}

# create Index

root@dlp:~ #
curl -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt -X PUT "https://127.0.0.1:9200/test_index"

Enter host password for user 'elastic':
{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}
# verify

root@dlp:~ #
curl -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/_aliases?pretty

Enter host password for user 'elastic':
{
  ".security-7" : {
    "aliases" : {
      ".security" : {
        "is_hidden" : true
      }
    }
  },
  "test_index" : {
    "aliases" : { }
  }
}

root@dlp:~ #
curl -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt https://127.0.0.1:9200/test_index/_settings?pretty

Enter host password for user 'elastic':
{
  "test_index" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test_index",
        "creation_date" : "1726619545200",
        "number_of_replicas" : "1",
        "uuid" : "MDldtWojT7ScXGXUzVmiIg",
        "version" : {
          "created" : "8500003"
        }
      }
    }
  }
}
[5] 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 -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt \
-H "Content-Type: application/json" \
-X PUT "https://127.0.0.1:9200/test_index/_doc/001" -d '{
    "subject" : "Test Post No.1",
    "description" : "This is the initial post",
    "content" : "This is the test message for using Elasticsearch."
}'
{"_index":"test_index","_id":"001","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

# show Mapping

root@dlp:~ #
curl -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_mapping/?pretty"

Enter host password for user 'elastic':
{
  "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 -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_doc/001?pretty"

Enter host password for user 'elastic':
{
  "_index" : "test_index",
  "_id" : "001",
  "_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 -u elastic --cacert /usr/local/etc/elasticsearch/certs/http_ca.crt "https://127.0.0.1:9200/test_index/_search?q=description:initial&pretty=true"

Enter host password for user 'elastic':
{
  "took" : 61,
  "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",
        "_id" : "001",
        "_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