代码改变世界

elasticsearch创建索引

2018-04-18 09:49  晨曦曙光  阅读(936)  评论(0编辑  收藏  举报

1.通过elasticsearch-head 创建

(1)登录localhost:9100

(2)点击复合查询

(3)输入内容

 

(4)勾选易读,点击验证是否是JSON格式

(5)点击提交请求,返回

{

  • "acknowledged": true

}

2.通过postman来创建索引:

(1)选择请求格式PUT,输入请求访问地址:127.0.0.1:9200/peoper

(2)选择下面的Body->raw->JSON(application/json)

(3)创建索引,例如:

{
 "settings":{
  "number_of_shards":3, //创建分片数
  "number_of_replicas":1//创建备份数
 },
 "mappings":{
  "man":{
   "properties":{
    "name":{
     "type":"text"
    },
    "country":{
     "type":"keyword"
    },
    "age":{
     "type":"integer"
    },
    "data":{
     "type":"date",
     "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    
    }
   }
  }
 }
}

(4)点击send ,如果格式正确会返回如下信息:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "peoper"
}

(5)现在索引就创建好了,返回elasticsearch-head 的页面刷新就能看到

 3.在postman中对索引进行插入数据

(1)选择访问请求为POST ,请求内容:127.0.0.1:9200/peoper/man/(也可以在后面跟上ID号,不跟是自动自增长ID)

(2)根据创建索引是创建的数据格式,插入数据如下:

{
 "name":"王尼玛",
 "country":"China",
 "age":35,
 "date":"1987-12-08"
}

如国数据添加成功会返回信息如下:

(3)在浏览器中点击刷新,就能够看到数据增加一条。点击数据浏览找到对应的索引就能看到插入数据的内容。

4.使用postman直接修改文档(指定修改文件的内容)

(1)请求访问类型为POST,请求访问内容输入:127.0.0.1:9200/peoper/man/1/_update(修改索引peoper下man对象ID为1的数据,后面的_update修改必须更上)

(2)修改内容如下:

{

 "doc":{  

 "name":"baing"  

}

}

其中修改的数据必须放在:“doc”:{}中

 5.使用脚本修改文档(使用postman)

(1)请求访问类型为POST,请求访问内容输入:127.0.0.1:9200/peoper/man/1/_update(修改索引peoper下man对象ID为1的数据,后面的_update修改必须更上)

(2)修改内容如下:

{  

"script":{   //使用脚本语言的类型

"lang":"painless",  //lang为语言,painless为内置的语言还可以是python

 "inline":"ctx._source.age += 15"  //获取当前年龄在加上15

 }

}

5.删除对应的数据

 6.查询

(1)简单查询:

在postman中选择GET  内容为127.0.0.1:9200/peoper/man/1

(2)条件查询

类型选择POST 内容为:127.0.0.1:9200/peoper/_search

查询条件:

{
"query":{
 "match_all":{}
}
}

这样就查出所有的内容

 

图中“from”表示从第几条数据开始,“size”表示返回一条数据

 

表示查询出标题中含有“elasticsearch”的内容通过“publish_date”这个字段进行降序

(3)聚合查询

图中"aggs"为聚合查询的关键自,"group_by_word_count"自定义根据字数查询的名字,“word_count”表示根据这个字段去查询统计

. 条件查询
{   
  "query":{
    "match":{
      "title":"elasticsearch"
    }
  },
  "from": 1,
  "size": 2,
  "sort":[{"publish_date":"desc"}]
}

match_all :表示查询所有 match : 表示条件查询 from : 表示返回结果从第几页开始 size : 表示返回结果的大小 sort : 表示排序

6. 聚合查询
{
  "aggs": {
    "group_by_word_count": {
      "terms":{
        "field":"word_count"
      }
    },
    "group_by_publish_date":{
      "terms":{
        "field":"publish_date"
      }
    }
  }
}

aggs: 表明是聚合查询 "group_by_word_count":自定义名称,可以随意 terms:关键字 field:使用的字段

7. 统计查询
{
"aggs": {
  "grand_word_count":{
    "stats":{
      "field":"word_count"
    }
  }
}
}

返回结果:

aggregations":{
"grand_word_count":{
"count": 8,
"min": 2000,
"max": 5000,
"avg": 3375,
"sum": 27000
}
}

说明: aggs:统计查询 grand_word_count:自定义名称 stats:统计方法,可以换成min/max/sum field:进行统计的字段

8. 高级查询

高级查询分为子条件查询和复合查询

1. 子条件查询:特定字段查询所指特定值
1. query context

在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的有多好.

常用查询:

  1. 全文本查询: 针对文本类型的查询

a. 模糊匹配:

​ post - http://127.0.0.1:9200/book/_search

{
  "query":{
    "match":{
      "title":"ElastichSearch入门"
    }
  }
}

​ 从结果中可以看出,结果会匹配ElasticSearch入门,他们的关系是或的关系,相当于自动分词

b. 习语匹配

{
  "query":{
    "match_phrase":{
      "title": "ElasticSearch入门"
    }
  }
}

从结果中可以看出,会把ElasticSearch入门当做一个整体的词进行匹配

c. 多个字段的模糊查询

{
  "query":{
    "multi_match":{
      "query":"瓦力",
      "fields":["author","title"]
    }
  }
}

d. querystring,语法查询()

{
  "query":{
    "query_string":{
      "query":"(ElasticSearch) AND 入门) OR Python"
    }
  }
}
{
  "query":{
    "query_string":{
      "query":"瓦力 OR ElasticSearch",
      "fields":["author","title"]
    }
  }
}

2). 字段级别的查询: 针对结构化数据,如数字,日期等

{
	"query":{
      "term":{
        "word_count":1000
      }
	}
}

term : 表示具体的字段查询

还可以指定范围:

{
  "query":{
    "range":{
      "word_count":{
        "gte": 1000,
        "lte": 2000
      }
    }
  }
}

关键词:range表明是范围查询,后面跟具体的字段,gte表示>=,lte表示<=

范围,还可以用在日期上.

2. filter context

在查询过程中,只判断该文档是否满足条件,只有Yes或No

{
  "query":{
    "bool":{
     "filter":{
      "term":{
        "word_count":1000
      }
     } 
    }
  }
}

filter结合bool使用

2. 复合条件查询:以一定的逻辑组合子条件查询
1. 固定分数查询
{
  "query":{
    "constant_score":{
      "filter":{
        "match":{
          "title":"ElasticSearch"
        }
      },
      "boost":2
    }
  }
}

constant_score:固定分数,即把_score的值指定,如果不加boost则为1,指定了boost的值,则_score等于boost的值

注意: constant_score不支持match

2. bool查询
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "author":"瓦力"
          }
        },
        {
          "match":{
            "title":"ElasticSearch"
          }
        }
      ]
    }
  }
}

should为关键词,应该满足他列出的条件,是或的关系

{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "author":"瓦力"
          }
        },
        {
          "match":{
            "title":"ElasticSearch"
          }
        }
      ]
    }
  }
}

must:与的关系

must和filter

{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "author":"瓦力"
          }
        },
        {
          "match":{
            "title":"ElasticSearch"
          }
        }
      ],
      "filter:[
        "term":{
          "word_count":1000
        }
      ]
    }
  }
}

即在满足must中的条件的同时,还有满足过滤条件的数据才会最终返回.

must的反义词mustnot

{
  "query":{
    "mustnot":{
      "term":{
        "author":"wali"
      }
    }
  }
}

一定不能满足该条件.

9. springboot集成ES

  1. 引入指定的版本

    		<dependency>
    			<groupId>org.elasticsearch.client</groupId>
    			<artifactId>transport</artifactId>
    			<version>5.5.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.elasticsearch</groupId>
    			<artifactId>elasticsearch</artifactId>
    			<version>5.5.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.apache.logging.log4j</groupId>
    			<artifactId>log4j-core</artifactId>
    			<version>2.7</version>
    		</dependency>
    

    transport 5.5.2 默认的不是ElasticSearch 5.5.2,要使用指定的版本必须声明ElasticSearch的版本,如果依然冲突,在transport中使用exclusions

  2. 配置

@Configuration
public class MyConfig {
    @Bean
    public TransportClient client() throws UnknownHostException {
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName("localhost"),
                9300 //tcp
        );
        
        Settings settings = Settings.builder()
                .put("cluster.name","wali")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);//可以增加多个节点
        return client;
    }
}
3. 相关操作
  @Autowired
  private TransportClient client;
  
  @GetMapping("/get/book/novel")
  @ResponseBody
  public ResponseEntity get(@RequestParam(value = "id", defaultValue = "") String id) {
      if (id.isEmpty())
          return new ResponseEntity(HttpStatus.NOT_FOUND);
      GetResponse result = client.prepareGet("book", "novel", id).get();
      if (!result.isExists()) {
          return new ResponseEntity(HttpStatus.NOT_FOUND);
      }
      return new ResponseEntity(result, HttpStatus.OK);
  }
  
  @PutMapping("/put/book/novel")
  @ResponseBody
  public ResponseEntity add(
          @RequestParam("title") String title,
          @RequestParam("author") String author,
          @RequestParam("word_count") int wordCount,
          @RequestParam("publish_date")
          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                  Date publishDate
  ) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      System.out.println(format.format(publishDate));
      try {
          XContentBuilder contentBuilder = 
                XContentFactory.jsonBuilder().startObject()
                .field("title", title)
                .field("author", author)
                .field("word_count", wordCount)
                .field("publish_date", format.format(publishDate))
                .endObject();
          System.out.println(contentBuilder.toString());
          IndexResponse result = 
                client.prepareIndex("book", "novel")
                      .setSource(contentBuilder).get();
          
          return new ResponseEntity(result.getId(), HttpStatus.OK);
      } catch (IOException e) {
          e.printStackTrace();
          return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
      }
  }
  
  @DeleteMapping("/delete/book/novel")
  @ResponseBody
  public ResponseEntity delete(@RequestParam("id") String id) {
      
      DeleteResponse result = 
           this.client.prepareDelete("book", "novel", id).get();
      return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
  }
  
  @PutMapping("/update/book/novel")
  @ResponseBody
  public ResponseEntity update(
          @RequestParam(value = "id", required = true) String id,
          @RequestParam(value = "title", required = false) String title,
          @RequestParam(value = "author", required = false) String author,
          @RequestParam(value = "word_count", required = false) 
                  Integer wordCount,
          @RequestParam(value = "publish_date", required = false)
          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                  Date publishDate
  ) {
      UpdateRequest updateRequest = new UpdateRequest("book", "novel", id);
      try {
          XContentBuilder contentBuilder =
                  XContentFactory.jsonBuilder().startObject();
          if (title != null)
              contentBuilder.field("title", title);
          if (author != null)
              contentBuilder.field("author", author);
          if (wordCount != null)
              contentBuilder.field("word_count", wordCount);
          if (publishDate != null)
              contentBuilder.field("publish_date",
              new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(publishDate));
          contentBuilder.endObject();
          updateRequest.doc(contentBuilder);
      } catch (IOException e) {
          e.printStackTrace();
          return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
      }
      
      try {
          UpdateResponse result = this.client.update(updateRequest).get();
          return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
      } catch (InterruptedException e) {
          e.printStackTrace();
          return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
      } catch (ExecutionException e) {
          e.printStackTrace();
          return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
      }
      
  }//update
  
  
  @PostMapping("/query/book/novel")
  @ResponseBody
  public ResponseEntity query(
          @RequestParam(value = "title", required = false) String title,
          @RequestParam(value = "author", required = false) String author,
          @RequestParam(value = "lt_word_count", required = false) Integer ltWordCount,
          @RequestParam(value = "gt_word_count", required = false, defaultValue = "0")
                  Integer gtWordCount
  ) {
      BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
      if (title != null)
          boolQuery.must(QueryBuilders.matchQuery("title", title));
      if (author != null)
          boolQuery.must(QueryBuilders.matchQuery("author", author));
      
      RangeQueryBuilder rangeQuery =
              QueryBuilders.rangeQuery("word_count")
                      .from(gtWordCount);
      if (ltWordCount != null)
          rangeQuery.to(ltWordCount);
      boolQuery.filter(rangeQuery);
      
      SearchRequestBuilder searchRequestBuilder =
              this.client.prepareSearch("book")
                      .setTypes("novel")
                      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                      .setQuery(boolQuery)
                      .setFrom(0)
                      .setSize(10);
      System.out.println(searchRequestBuilder);
      SearchResponse searchResponse = searchRequestBuilder.get();
      
      List<Map<String, Object>> result = new ArrayList<>();
      
      for (SearchHit searchHit : searchResponse.getHits()) {
          result.add(searchHit.getSource());
      }
      return new ResponseEntity(result, HttpStatus.OK);
  }