Good programmers to share large data line learning technology ELK

Good programmers to share large data line learning ELK technology, bin storage elasticSearch  run the command

         config  store configuration files

         lib  storage elasticSearch operational dependency jar package

         modules  stored elasticSearch  module

         plugins  storage plug-in 

 

1.1 Elasticsearch and Mysql contrast

Elasticsearch cluster can contain multiple index (Index), each index can contain more than one type (Type), each type can contain multiple documents (Document), each document can contain multiple fields (Field). The following are MySQL and Elasticsearch term analogy diagram to help understand:


As with MySQL Database must be specified as, to use Elasticsearch first need to create Index:

client.indices.create({index : 'blog'});

This creates a blog called the Index. Type Instead of creating separate, you can specify when you create Mapping. Mapping Document used to define the type of each field, i.e. Analyzer is used, whether the index attributes, and the like is critical.

 

Index object (blob):  table structure for storing data  , any search data, stored in the index object.

         Mapping (mapping): How data is stored on the index object, there is a need for mapping configuration , including: type of data, whether stored, whether the word ... and so on.

         Document (document): a data record, the presence of the index object

         Document type (type): an index to store multiple types of data objects, the data identified by document type 

[] The follow-up program: 

Step one: Create an index object

Step two: establish a mapping

The third step: to store data [documentation]

Step four: Specify the type of document search data [documentation]

 

1.1 Creating an index

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

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

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

View

 

1.1 Insert a document

To create a type in / blog01 index, can be inserted into a document.

To include "Deck the Halls" document into the index, run the following command (the command and other CURL command in this tutorial are typed into one line):

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


前面的命令使用 PUT 动词将一个文档添加到 /article文档类型,并为该文档分配 ID 为1。URL 路径显示为index/doctype/ID(索引/文档类型/ID)。

1.2. 查看文档

要查看该文档,可使用简单的 GET 命令:

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


Elasticsearch 使用你之前 PUT 进索引中的 JSON 内容作为响应:

1.3. 更新文档

如果你认识到title字段写错了,并想将它更改为 Whatislucene 怎么办?可运行以下命令来更新文档:

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


因为此命令使用了相同的唯一 ID为1,所以该文档会被更新。

1.4. 搜索文档

是时候运行一次基本查询了,此查询比你运行来查找 “Get the Halls” 文档的简单 GET 要复杂一些。文档 URL 有一个内置的 _search 端点用于此用途。在标题中找到所有包含单词 lucene 的数据:

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


 参数表示一个查询。

1.5. 检查搜索返回对象

    上图中给出了 Elasticsearch 从前面的查询返回的数据。

    在结果中,Elasticsearch 提供了多个 JSON 对象。第一个对象包含请求的元数据:看看该请求花了多少毫秒 (took) 和它是否超时 (timed_out)_shards 字段需要考虑 Elasticsearch 是一个集群化服务的事实。甚至在这个单节点本地部署中,Elasticsearch 也在逻辑上被集群化为分片。在往后看可以观察到 hits 对象包含:

·        total 字段,它会告诉你获得了多少个结果

·        max_score,用于全文搜索

·        实际结果

实际结果包含 fields 属性,因为你将 fields 参数添加到了查询中。否则,结果中会包含 source,而且包含完整的匹配文档。_index_type  _id 分别表示索引、文档类型、ID_score 指的是全文搜索命中长度。这 个字段始终会在结果中返回。

1.6. 删除文档

暂时不要删除该文档,知道如何删除它就行了:

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


1.7. 删除索引

暂时不要删除该文档,知道如何删除它就行了:

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


1.要使用 Elasticsearch 首先需要创建 Index:client.indices.create({index : 'blog'});创建了一个名为 blog的 Index

2.Type 不用单独创建,在创建 Mapping 时指定就可以。

3.Mapping 用来定义 Document 中每个字段的类型,即所使用的 analyzer、是否索引等属性,非常关键等

URL 路径显示为index/doctype/ID(索引/文档类型/ID)

创建文档(插入一条数据),自动创建索引和映射

 

======================================================

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 {

    //创建连接

    private Client client;

 

    /**

     * 通过TransportClient获取ES连接

     */

    @Before

    public void getClient() throws Exception {

        //ES服务的JavaAPI的port为9300

        //注意:如果请求一个ES集群,可以考虑多添加几个节点,

        //为了避免在一个节点出现网络问题导致的请求失败问题,可以自动切换另外一个节点

        client = TransportClient.builder().build()

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

 

 

    }

    @Test

        /**

         * 1.使用json来创建文档(插入一条数据),自动创建索引和映射

         */

 

        public void creatDocument() {

        //jason格式的数据创建文档(插入一条数据),自动创建索引和映射

        String source = "{" +

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

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

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

                "}";

        //创建文档:定义索引名称,文档类型,主键唯一标识id

        //execute().actionGet()==get() 代码马上执行

        IndexResponse indexResponse =

                client.prepareIndex("blog""article""1").setSource(source).get();

 

        //获取响应信息

        this.loadResponse(indexResponse);

        client.close();

    }

    public void loadResponse(IndexResponse indexResponse){

        System.out.println("索引名称" + indexResponse.getIndex());

        System.out.println("文档类型" + indexResponse.getType());

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

        System.out.println("版本" + indexResponse.getVersion());

        System.out.println("是否创建成功" + indexResponse.isCreated());

    }

        /**

         * 2.使用mao创建文档.自动创建索引和映射

         */

        public  void creatDocument2(){

            //map类型的数据

            Map<String,Object> source = new HashMap<String, Object>();

            source.put("id","2");

            source.put("title","我是谁");

            source.put("content","我在哪");

 

            //创建文档

            IndexResponse indexResponse =

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

                            .setSource(source).get();

            this.loadResponse(indexResponse);

 

            client.close();

        }

 

 

    /**

     * 3.使用ES帮助类(执行类),创建文档

     */

    @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());

 

        //创建文档

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

 

        this.loadResponse(indexResponse);

        client.close();

 

 

 

    }

}

 

======================================================================================

搜索文档数据

1.单个索引

 

2.多个索引


更新数据

方式一:


方式二:


方式三


删除数据


查询

queryStringQuery:


es默认的分词器并没有中文进行分词,需要我们按照一个比默认屌很多的分词器(Ik分词器)

 

创建映射



Guess you like

Origin blog.51cto.com/14479068/2428936