8.9. Elasticsearch internals¶
See also
- Database Schema
Elasticsearch index schema definitions and details
- PEAT Elasticsearch indices reference
Table of the Elasticsearch indices used by PEAT
- Elasticsearch
Elasticsearch usage and other information.
8.9.1. Notes¶
PEAT follows the Elastic Common Schema (ECS), and any changes must adhere to the ECS (when possible)
All indices share the ECS Base and Agent field sets (refer to Database Schema)
ALL timestamps are in the UTC timezone
Field types (the “Type” column in the tables) are Elasticsearch datatypes (reference). When storing as a plain JSON file, ensure the format it is stored in either matches or can be cohered to the corresponding ES format.
The document’s
_idfield is unique for each document. The format is:peat~<run-id>~<microsecond>, where<microsecond>is an integer.Sub-fields are nested JSON objects. From the ECS Guidelines: “The document structure should be nested JSON objects. If you use Beats or Logstash, the nesting of JSON objects is done for you automatically. If you’re ingesting to Elasticsearch using the API, your fields must be nested objects, not strings containing dots.”
8.9.2. Code documentation¶
8.9.2.1. Elastic¶
- class Elastic(server_url='http://localhost:9200/')[source]¶
Wrapper for interacting with an Elasticsearch or OpenSearch database.
- property es: Elasticsearch | OpenSearch¶
Elasticsearch or OpenSearch client instance.
If it doesn’t exist yet, this will create a client object and connect to the server. Otherwise, will return the existing instance.
- doc_exists(index, doc_id)[source]¶
Check if a document exists on an index.
Note: this won’t auto-resolve dated index names.
- Return type:
- index_exists(index)[source]¶
Check if an Elasticsearch/OpenSearch index exists.
This method caches index existence checks to reduce number of requests to the server.
- create_index(index, fields_limit=20000)[source]¶
Create an index in Elasticsearch/OpenSearch if it doesn’t already exist.
- Parameters:
index (
str) -- Name of the index to createfields_limit (
int) -- Elastic limits the number of fields in an index to 1000 by default, which is problematic for some devices that have protocol register mappings (e.g. DNP3, Modbus). To avoid this, we raise the limit by default for all PEAT indices. This option allows us to tweak that limit as needed for specific indices.
- Return type:
- Returns:
If the index was successfully created
- search(index, query=None, body=None)[source]¶
Query for values from an index.
Note
By default, results sorted are in descending order by timestamp
- Parameters:
index (
str) -- Index to searchSearch query, as either a string in Lucene Query format, or a
dictin Elasticsearch Query DSL format. IfqueryisNone, all values in the index will be returned. Resources:body (
dict|None) -- “body” argument, in lieu of query. Use this if you’re doing more complicated operations, like aggregations. Example:body = {"query": {...}, "aggs": {...}}
- Return type:
- Returns:
List of results, in descending order by timestamp (unless a custom body is provided with a custom “sort” argument). The
listwill be empty if there were no results or an error occurred.
- raw_search(search_args)[source]¶
Query for data in Elasticsearch.
Assumes you know what you’re doing and want direct access to the API.
- gen_body(content)[source]¶
Generate the basic body of doc to be pushed to Elasticsearch, auto-populating standard fields such as “observer”, “@timestamp”, etc.
- bulk_push(index, contents)[source]¶
Upload multiple docs to an Elasticsearch index.
Note
Index names will have a date appended, unless
no_date=True. For example,peat-configswill becomepeat-configs-2020.01.01.
- push(index, content, doc_id=None, no_date=False)[source]¶
Upload data to an Elasticsearch index.
Note
Index names will have a date appended, unless
no_date=True. For example,peat-configswill becomepeat-configs-2020.01.01.- Parameters:
- Return type:
- Returns:
True if the push was successful, False if there was an error or if index creation failed.
- static pickle(obj)[source]¶
Pickle a Python object into an Elastic-friendly Base64 string.
- Return type:
8.9.2.2. Index type mappings¶
Mappings (the Elastic “schema”) for the various PEAT Elasticsearch indices.
These encode the types defined in the Elastic schemas for integration of third-party tools and other Sandia capabilities. Schema reference: Database Schema
Note
Type is only required for individual fields, NOT documents
Data structure
Key: Name of the index (e.g.
ot-device-hosts-timeseries).- Value: The field mapping for the index, including field types
and other field configurations, such as tokenizers or filters.
Official Elasticsearch documentation and references