• postman 安装,对elasticsearch进行请求


    1  使用postman对elasticsearch进行测试

    :下载插件: https://www.getpostman.com/apps ,下载时exe文件,双击自动安装,首次打开注册。下面就可以使用进行测试

        请求:

      响应:

     

    2:elasticsearch中可以使用post,put,get等请求方式,索引原理:https://my.oschina.net/90888/blog/1617292

     3:对elasticsearch操作有,post新增索引,数据,put更新索引,数据,get获取,分词等 

          3.1:新增一条索引,数据,index只能小写。

                   

        新增结果:

               

    如果post中没有id值,elasticsearch将会自动创建id,

    3.2  查询数据

    往查询url POST数据即可:

    URL格式:http://xxxhost:8201/qa_xx2/qa_xx3/_search

    a. 查询title中包含有my字段的文档。Highlight设置高亮命中的词。POST方法的body:

           

    {
        "query": {
            "match": {  
                "title": {   //字段
                    "query": "my "
                }
            }
        },
        "highlight": {
            "fields": { 
                "title": {    //查询到的文档进行高亮
                    
                }
            }
        }
    }

    结果:查询成功

    {
        "took": 3,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 0.2876821,
            "hits": [
                {
                    "_index": "indextest",
                    "_type": "type",
                    "_id": "i_WWCGIBMRJTfK5J3FTg",
                    "_score": 0.2876821,  
                    "_source": {   //源文档
                        "title": "this is my fisrt elasticsearch test",
                        "content": "Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyze big volumes of data quickly and in near real time. It is generally used as the underlying engine/technology that powers applications that have complex search features and requirements.",
                        "desc": "结束,字段名相当于列名"
                    },
                    "highlight": {  //高亮显示
                        "title": [
                            "this is <em>my</em> fisrt elasticsearch test"
                        ]
                    }
                }
            ]
        }
    }

     4  使用javaapi对elasticsearch进行操作

         4.1  添加maven依赖  build.gradle

                

    'org.elasticsearch:elasticsearch:6.0.0',
                'org.elasticsearch.client:transport:6.0.0',
                'com.alibaba:fastjson:1.2.44',

    4.2进行测试

        @Test
        public void getData() {
            try {
                Settings settings = Settings.builder().put("cluster.name", "my-esLearn").build();
                //创建client
                client = new PreBuiltTransportClient(settings)
                        .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
                //搜索数据  blog 索引,article,类型,1 id
                GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet();
                //输出结果
                System.out.println(response.getSource());
                //关闭client
                client.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

         

     结果:

     5:使用java api对elasticsearch进行请求并指定ik分词器

         

       @Test
        public void test2() throws IOException {
    
            BulkRequestBuilder bulkRequest = client.prepareBulk();
    
    // either use client#prepare, or use Requests# to directly build index/delete requests
            bulkRequest.add(client.prepareIndex("twitter", "tweet", "1")  //创建索引
                    .setSource(jsonBuilder()   //请求体,数据
                            .startObject()
                            .field("analyzer","ik_max_word")  //指定分词器
                            .field("user", "kimchy")
                            .field("postDate", new Date())
                            .field("message", "trying out Elasticsearch")
                            .endObject()
                    )
            );
    
            bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("user", "kimchy")
                            .field("postDate", new Date())
                            .field("message", "another post")
                            .endObject()
                    )
            );
    
            BulkResponse bulkResponse = bulkRequest.get();
            if (bulkResponse.hasFailures()) {
                // process failures by iterating through each bulk response item
            }
    
            BulkItemResponse[] items = bulkResponse.getItems();  //一次发送多个请求,会返回一个响应数组。
            DocWriteResponse response = items[0].getResponse();
            String index = response.getIndex();
            System.out.println(index);  //结果  twitter
        }

     参考:https://www.cnblogs.com/cswuyg/p/5651620.html

    javaapi 操作elasticsearch  http://blog.csdn.net/zjcjava/article/details/78659721

       英文 文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

     Elastic日报 第205期 (2018-03-09)
    1. Elasticsearch在电商领域的实战应用
    http://t.cn/REEzwES
    2.使用Docker和Elasticsearch搭建全文本搜索引擎应用
    http://t.cn/REEzUng
    3.剖析Elasticsearch索引原理
    https://my.oschina.net/90888/blog/1617292

    编辑:铭毅天下
    归档:https://elasticsearch.cn/article/525
    订阅:https://tinyletter.com/elastic-daily 
  • 相关阅读:
    switch 是否能作用在byte 上,是否能作用在long 上,是否能作用在String上?
    int和Integer有什么区别?
    访问修饰符public,private,protected,以及不写(默认)时的区别?
    Hashmap如何同步?
    Iterator和ListIterator的区别?
    Java的HashMap是如何工作的?
    MyBatis与Hibernate有哪些不同?
    Spring框架中有哪些不同类型的事件?
    Spring的自动装配?
    Spring如何处理线程并发问题?
  • 原文地址:https://www.cnblogs.com/liyafei/p/8530522.html
Copyright © 2020-2023  润新知