1.参考中文官网查看详细语法内容

点击去官网入门级别的用法

2.mapping声明:

1.如果不声明字段的mapping,ES会自动根据类型自动的声明。
关于mapping的好文章 添加type的时候指定各个字段的类型

# 先删除原来的document
DELETE testDocument
# 然后添加新数据,设置mapping
PUT testDocument
{
  "mappings": {
    "article" : {
      "properties":
      {
        "title" : {"type": "text"} , 
        "author" : {"type": "text"} , 
        "titleScore" : {"type": "double"} 
        
      }
    }
  }
}
#  查询索引数据类型
get testDocument/article/_mapping

2.改变数据字段的类型:
ES是不允许你修改字段的类型,所以需要重新创建一个index库,然后拷贝数据

  • 第一步: 重新创建一个索引库document1, 失去精准的字段设置mapping
PUT document1
{
  "mappings": {
    "article" : {
      "properties":
      {
        "title" : {"type": "text"} , 
        "author" : {"type": "text"} , 
        "titleScore" : {"type": "double"} 
      }
    }
  }
}
  • 然后用reindex的命令, 将原始库的内容,拷给到document1中
POST _reindex
{
  "source": {
    "index": "document"
  },
  "dest": {
    "index": "document1"
  }
}

3.IK分词器的使用:

友情链接:
1.使用analyzer设置为ik_max_word

GET /_analyze
{
  "analyzer":"ik_max_word",
  "text":"我是一名架构师"
}

ik_max_word:细致的划分(索引数据时候使用 搜索文章之类的)
ik_smart:粗虐的划分(用户查询的时候 不用分很细的东西)

4.创建一个index以及type及其映射:

创建了一个index为test,表为jd的索引
title设置为ik中文分析器的ik_max_word
shopName设置为ik中文分析器的ik_smart

PUT /test/
{
  "mappings":{
    "jd": {
        "properties": {
            "id": {
                "type": "long"
            },
            "title": {
                "type": "text",
                "search_analyzer": "ik_max_word"
                "analyzer": "ik_max_word"
            },
            "image":{
              "type": "keyword"
            },
            "priceMin":{
              "type": "double"
            },
            "priceMax":{
              "type": "double"
            },
            "produceSrc":{
              "type": "keyword"
            },
            "shopName": {
                "type": "text",
                "analyzer": "ik_smart"
                "search_analyzer": "ik_smart"
            },
            "shopNameUri":{
              "type":"keyword"
            },
            "commitCount":{
              "type":"long"
            },
            "goodCount":{
              "type":"long"
            },
            "badCount":{
              "type":"long"
            }
        }
    }
  }
}

主要的映射 链接

字段类型概述
一级分类	二级分类	具体类型
核心类型	字符串类型	text(当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项)
				keyword(keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过**精确值**搜索到。)
整数类型	integer,long,short,byte
浮点类型	double,float,half_float,scaled_float
逻辑类型	boolean
日期类型	date
范围类型	range
二进制类型	binary
复合类型	数组类型	array
对象类型	object
嵌套类型	nested
地理类型	地理坐标类型	geo_point
地理地图	geo_shape
特殊类型	IP类型	ip
范围类型	completion
令牌计数类型	token_count
附件类型	attachment
抽取类型	percolator

5.springboot使用elasticsearch

1.springboot有两种方式与elasticsearch交互

  1. 通过jest的方式,默认是不启动的
    1).首先创建springboot的项目,选中web以及nosql的elasticsearch
    2).因为使用的是是jest的方式,需要把elasticsearch依赖删除,导入jset依赖
    3).如果elasticsearch服务器不在本地,需要配置地址
#配置jest
spring:
  elasticsearch:
    jest:
      uris: http://192.168.0.113:9200

4).创建实体类

package com.wcy.elasticsearch.bean;

import io.searchbox.annotations.JestId;

import java.lang.annotation.Documented;


public class Empleyee {
    @JestId
    private Integer id;
    private String name;
    private String desc;

    public Empleyee() {

    }

    public Empleyee(Integer id, String name, String desc) {
        this.id = id;
        this.name = name;
        this.desc = desc;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

5).存入数据

@Autowired
    JestClient jestClient;//引入jestClient
    //jest添加数据
    @Test
    void contextLoads() {
        Empleyee empleyee=new Empleyee(1,"王长印","喜欢游泳、上网、打游戏");
        Index index=new Index.Builder(empleyee).index("wangchangyin").type("empleyee").build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

6).查找数据

@Test
    public void SearchJest(){
        String json="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"name\" : \"王长印\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        Search search=new Search.Builder(json).addIndex("wangchangyin").addType("empleyee").build();
        try {
            SearchResult execute = jestClient.execute(search);
            System.out.println(execute.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2. **通过springData elasticsearch的方式

**
1).引入springData elasticsearch依赖
2).配置elasticsearch

spring:
  #springData elasticsearch的方式
  data:
    elasticsearch:
      cluster-name: docker-cluster #默认为elasticsearch
      cluster-nodes: 192.168.0.113:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
      properties:
        path:
          logs: ./elasticsearch/log #elasticsearch日志存储目录
          data: ./elasticsearch/data #elasticsearch数据存储目录

3).实体类:

package com.wcy.elasticsearch.bean;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "bookindex",type = "book")
public class Book {
    private Integer id;
    private String name;
    private String author;

    public Book() {
    }

    public Book(Integer id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

4).dao层 可以自定义查询方法

package com.wcy.elasticsearch.repository;

import com.wcy.elasticsearch.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * 与数据库的dao相似
 */
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    public List<Book> findBookByName(String name);//自定义查询方式
}

5).添加数据

@Autowired
    private BookRepository bookRepository;
    /**
     * springData elasticsearch的添加
     */
   @Test
    public void testSearch(){
        Book book=new Book(2,"Java从入门到秃头","小王");
        bookRepository.index(book);
    }

6).查询数据

@Test
    public void testSearch2(){
       //查询方法一
/*        String queryString = "java";//搜索关键字
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString);
        Iterator<Book> iterator = bookRepository.search(builder).iterator();
        while (iterator.hasNext()){
            Book next = iterator.next();
            System.out.println(next);
        }*/

        //查询方法二
        List<Book> java = bookRepository.findBookByName("java");
        for (Book book:java){
            System.out.println(book);
        }

    }

3.elasticsearch的更多知识:

1.String的映射默认是全文索引,当然可以设置 index 属性

index 属性控制怎样索引字符串。它可以是下面三个值:
analyzed(默认) 首先分析字符串,然后索引它。换句话说,以全文索引这个域。
not_analyzed 索引这个域,所以它能够被搜索,但索引的是精确值。不会对它进行分析。
no 不索引这个域。这个域不会被搜索到。

如果想要String精准匹配 可以通过设置String的index为not_analyzed
2.String的分析器,我们可以使用内置的分析器

对于 analyzed 字符串域,用 analyzer 属性指定在搜索和索引时使用的分析器。默认, Elasticsearch 使用 standard 分析器, 但你可以指定一个内置的分析器替代它,例如 whitespace 、 simple 和 english:

列如字段tweet设置分析器为以英语分析

{
    "tweet": {
        "type":     "string",
        "analyzer": "english"
    }
}