Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Big data, a good programmer, shares ELK technology with his learning route.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good programmer big data learns the route to share ELK technology, and bin stores elasticSearch run commands

Config stores configuration files

Lib stores elasticSearch to run depending on jar package

Modules stores elasticSearch module

Plugins storage plug-in

1.1.The comparison between Elasticsearch and Mysql

An Elasticsearch cluster can contain multiple indexes (Index), each index can contain multiple types (Type), each type can contain multiple documents (Document), and each document can contain multiple fields (Field). The following is a term analogy diagram for MySQL and Elasticsearch to help you understand:

Just as you must specify Database to use MySQL, you first need to create an Index to use Elasticsearch:

Client.indices.create ({index: 'blog'})

This creates an Index named blog. The Type does not need to be created separately, it can be specified when you create the Mapping. Mapping is used to define the type of each field in the Document, that is, the analyzer used, whether indexed or not, and so on.

Index object (blob): the table structure in which data is stored. Any search data is stored on the index object.

Mapping (mapping): how the data is stored on the index object requires a mapping configuration, including: data type, whether to store, whether to segment words. Wait.

Document: a data record that exists on an indexed object

Document type (type): an indexed object stores multiple types of data, which are identified by the document type

[subsequent programming]:

Step 1: create indexed objects

Step 2: establish a mapping

Step 3: store data [documents]

Step 4: specify the document type to search for data [document]

1.1. Create an index

The general format of the Elasticsearch command is: REST VERBHOST:9200/index/doc-type- where REST VERB is PUT, GET, or DELETE. (use the curlL-X verb prefix to explicitly specify the HTTP method. )

To create an index, run the following command in your shell:

Curl-XPUT "http://localhost:9200/blog01/"

View

1.1. Insert a document

To create a type under the / blog01 index, insert a document.

To insert a document containing "Deck the Halls" into the index, run the following command (type this command and other CURL commands for this tutorial on one line):

Curl-XPUT "http://localhost:9200/blog01/article/1"-d" {"id": "" 1 "", "title": "Whatiselasticsearch"} ""

The previous command uses the PUT verb to add a document to the / article document type and assigns the document an ID of 1. The URL path is displayed as index/doctype/ID (index / document type / ID).

1.2. View the document

To view the document, use a simple GET command:

Curl-XGET "http://localhost:9200/blog01/article/1"

Elasticsearch responds with the JSON content in your previous PUT index:

1.3. Update the document

What if you realize that the title field is miswritten and want to change it to Whatislucene? You can run the following command to update the document:

Curl-XPUT "http://localhost:9200/blog01/article/1"-d" {"id": "" 1 "", "title": "Whatislucene"} ""

Because this command uses the same unique ID of 1, the document is updated.

1.4. Search for documents

It's time to run a basic query that is a little more complex than the simple GET you run to find the "Get the Halls" document. The document URL has a built-in _ search endpoint for this purpose. Find all the data that contains the word lucene in the title:

Curl-XGET "http://localhost:9200/blog01/article/_search?q=title:'Whatislucene'"

Parameter represents a query.

1.5. Check the objects returned by search

The figure above shows the data returned by Elasticsearch from the previous query.

In the result, Elasticsearch provides multiple JSON objects. The first object contains the metadata for the request: see how many milliseconds the request took (took) and whether it timed out (timed_out). The _ shards field needs to take into account the fact that Elasticsearch is a clustered service. Even in this single-node local deployment, Elasticsearch is logically clustered into shards. Looking back, you can see that the hits object contains:

The total field, which will tell you how many results have been obtained

Max_score for full-text search

Actual result

The actual result contains the fields attribute because you added the fields parameter to the query. Otherwise, the result will contain the source and the complete matching document. _ index, _ type, and _ id denote index, document type, and ID;_score refer to the full-text search hit length, respectively. These four fields are always returned in the result.

1.6. Delete document

Don't delete the document yet, just know how to delete it:

Curl-XDELETE "http://localhost:9200/blog01/article/1"

1.7. Delete index

Don't delete the document yet, just know how to delete it:

Curl-XDELETE "http://localhost:9200/blog01"

1. To use Elasticsearch, you first need to create an Index:client.indices.create ({index: 'blog'}); create an Index named blog

The 2.Type does not need to be created separately, it can be specified when you create the Mapping.

3.Mapping is used to define the type of each field in the Document, that is, the analyzer used, whether indexed, etc., which is very critical, etc.

The URL path is displayed as index/doctype/ID (index / document type / ID)

Create a document (insert a piece of data), automatically create indexes and maps

=

Import org.elasticsearch.action.index.IndexResponse

Import org.elasticsearch.client.Client

Import org.elasticsearch.client.transport.TransportClient

Import org.elasticsearch.common.transport.InetSocketTransportAddress

Import org.elasticsearch.common.xcontent.XContentBuilder

Import org.elasticsearch.common.xcontent.XContentFactory

Import org.junit.Before

Import org.junit.Test

Import java.net.InetAddress

Import java.util.HashMap

Import java.util.Map

Public class ESTest {

/ / create a connection

Private Client client

/ * *

* obtain ES connection through TransportClient

, /

@ Before

Public void getClient () throws Exception {

/ / the port of the JavaAPI of ES service is 9300

/ / Note: if you request an ES cluster, you can consider adding several more nodes

/ / in order to avoid request failure caused by network problems on one node, you can automatically switch another node.

Client = TransportClient.builder () .build ()

.addTransportAddress (new InetSocketTransportAddress (InetAddress.getByName ("localhost"), 9300))

}

@ Test

/ * *

* 1. Use json to create documents (insert a piece of data), automatically create indexes and maps

, /

Public void creatDocument () {

/ / data in jason format to create documents (insert a piece of data), automatically create indexes and maps

String source = "{" +

"\" id\ ":\" 1 "," +

"\ title\":\ "woshishui\", "+

"\" content\ ":\" wozaina\ "+

"}"

/ / create a document: define the index name, document type, and the primary key uniquely identifies id

/ / execute () .actionGet () = = get () code is executed immediately

IndexResponse indexResponse =

Client.prepareIndex ("blog", "article", "1") .setSource (source) .get ()

/ / get response information

This.loadResponse (indexResponse)

Client.close ()

}

Public void loadResponse (IndexResponse indexResponse) {

System.out.println (Index name + indexResponse.getIndex ())

System.out.println ("document type" + indexResponse.getType ())

System.out.println ("ID" + indexResponse.getId ())

System.out.println ("version" + indexResponse.getVersion ())

System.out.println ("created successfully" + indexResponse.isCreated ())

}

/ * *

* 2. Use mao to create documents. Automatically create indexes and mappings

, /

Public void creatDocument2 () {

/ / data of type map

Map source = new HashMap ()

Source.put ("id", "2")

Source.put ("title", "who am I")

Source.put ("content", "where am I")

/ / create a document

IndexResponse indexResponse =

Client.prepareIndex ("blog", "article", "2")

.setSource (source) .get ()

This.loadResponse (indexResponse)

Client.close ()

}

/ * *

* 3. Use the ES help class (execution class) to create a document

, /

@ Test

Public void creatDocument3 () throws Exception {

XContentBuilder source = XContentFactory.jsonBuilder ()

.startObject ()

Field ("id", 3)

Field ("title", "whoami")

Field ("content", "whereami")

.endObject ()

System.out.println (source.toString ())

/ / create a document

IndexResponse indexResponse = client.prepareIndex ("blog", "article", "3") .setSource (source) .get ()

This.loadResponse (indexResponse)

Client.close ()

}

}

=

Search document data

1. Single index

two。 Multiple indexes

Update data

Method 1:

Method 2:

Mode 3

Delete data

Query

QueryStringQuery:

Es's default word splitter does not have Chinese word segmentation, so we need to follow a much better word splitter than the default (Ik word splitter).

Create Mappin

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report