Elasticsearch (2) - 映射

经常使用映射类型java

核心的字段类型以下:程序员

Stringspring

字符串包括text和keyword两种类型:编程

一、textapi

analyzer数组

经过analyzer属性指定分词器。浏览器

下边指定name的字段类型为text,使用ik分词器的ik_max_word分词模式。springboot

"name": { "type": "text", "analyzer":"ik_max_word" }

上边指定了analyzer是指在索引和搜索都使用ik_max_word,若是单独想定义搜索时使用的分词器则能够经过search_analyzer属性。app

对于ik分词器建议是索引时使用ik_max_word将搜索内容进行细粒度分词,搜索时使用ik_smart提升搜索精确性。框架

"name": {
    "type": "text",
    "analyzer":"ik_max_word",
    "search_analyzer":"ik_smart"
}

index

经过index属性指定是否索引。

默认为index=true,即要进行索引,只有进行索引才能够从索引库搜索到。

可是也有一些内容不须要索引,好比:商品图片地址只被用来展现图片,不进行搜索图片,此时能够将index设置为false。

删除索引,从新建立映射,将pic的index设置为false,尝试根据pic去搜索,结果搜索不到数据

"pic": {
    "type": "text",
    "index":false
}

store

是否在source以外存储,每一个文档索引后会在 ES中保存一份原始文档,存放在"_source"中,通常状况下不须要设置store为true,由于在_source中已经有一份原始文档了。

测试

删除xc_course/doc下的映射

建立新映射:Post http://localhost:9200/xc_course/doc/_mapping

{
    "properties": {
        "name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        },
        "description": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        },
        "pic": {
            "type": "text",
            "index": false
        },
        "studymodel": {
            "type": "text"
        }
    }
}

插入文档:

http://localhost:9200/xc_course/doc/4028e58161bcf7f40161bcf8b77c0000

{
    "name": "Bootstrap开发框架",
    "description": "Bootstrap是由Twitter推出的一个前台页面开发框架,在行业之中使用较为普遍。此开发框架包含了大量的CSS、 JS程序代码, 能够帮助开发者( 尤为是不擅长页面开发的程序人员) 轻松的实现一个不受浏览器限制的精美界面效果。 ",
    "pic": "group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg",
    "studymodel": "201002"
}

查询测试:

Get http://localhost:9200/xc_course/_search?q=name:开发
Get http://localhost:9200/xc_course/_search?q=description:开发
Get http://localhost:9200/xc_course/_search?
q=pic:group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg
Get http://localhost:9200/xc_course/_search?q=studymodel:201002
经过测试发现:name和description都支持全文检索,pic不可做为查询条件。

2 keyword

keyword字段为关键字字段,一般搜索keyword是按照总体搜索,因此建立keyword字段的索引时是不进行分词的,好比:邮政编码、手机号码、身份证等。keyword字段一般用于过虑、排序、聚合等。

更改映射

{
    "properties": {
        "studymodel": {
            "type": "keyword"
        },
        "name": {
            "type": "keyword"
        }
    }
}

插入文档:

{
    "name": "java编程基础",
    "description": "java语言是世界第一编程语言,在软件开发领域使用人数最多。",
    "pic": "group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg",
    "studymodel": "201001"
}

根据studymodel查询文档

搜索:http://localhost:9200/xc_course/_search?q=name:java

name是keyword类型,因此查询方式是精确查询

date日期类型

日期类型不用设置分词器。一般日期类型的字段用于排序。

format

经过format设置日期格式

例如设置容许date字段存储年月日时分秒、年月日及毫秒三种格式。

{
    "properties": {
        "timestamp": {
            "type": "date",
            "format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd"
        }
    }
}

插入文档

{

    "name": "spring开发基础",
    "description": "spring 在java领域很是流行,java程序员都在用。",
    "studymodel": "201001",
    "pic": "group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg",、
    "timestamp": "2018‐07‐04 18:28:58"

}

数值类型

例如,建立以下映射

post:http://localhost:9200/xc_course/doc/_mapping

{
    "properties": {
        "description": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        },
        "name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        },
        "pic": {
            "type": "text",
            "index": false
        },
        "price": {
            "type": "float"
        },
        "studymodel": {
            "type": "keyword"
        },
        "timestamp": {
            "type": "date",
            "format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"
        }
    }
}

插入文档:

Post: http://localhost:9200/xc_course/doc/1

{
    "name": "Bootstrap开发",
    "description": "Bootstrap是由Twitter推出的一个前台页面开发框架, 是一个很是流行的开发框架, 此框架集成了多种页面效果。 此开发框架包含了大量的CSS、 JS程序代码, 能够帮助开发者( 尤为是不擅长页面开发的程序人员) 轻松的实现一个不受浏览器限制的精美界面效果。 ", 
    "studymodel ": "201002 ",
    "price ":38.6, 
    "timestamp ":"2019 - 05 - 07 19: 11: 35 ",
    "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAA Jx5ZjNDEM428.jpg"
}

springboot客户端链接Demo

ES提供多种不一样的客户端:

一、TransportClient

ES提供的传统客户端

二、RestClient

RestClient是官方推荐使用的,它包括两种:Java Low Level REST Client和 Java High Level REST Client。

添加依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch‐rest‐high‐level‐client</artifactId>
    <version>6.2.1</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.2.1</version>
</dependency>

配置文件

server:
    port: ${port:40100}
spring:
    application:
        name: xc‐search‐service
xuecheng:
    elasticsearch:
        hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔    

配置类

public class ElasticsearchConfig {

    @Value("${xuecheng.elasticsearch.hostlist}")
    private String hostlist;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //解析hostlist配置信息
        String[] split = hostlist.split(",");
        //建立HttpHost数组,其中存放es主机和端口的配置信息
        HttpHost[] httpHostArray = new HttpHost[split.length];
        for(int i=0;i<split.length;i++){
            String item = split[i];
            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
        }
        //建立RestHighLevelClient客户端
        return new RestHighLevelClient(RestClient.builder(httpHostArray));
    }

    @Bean
    public RestClient restClient(){
        //解析hostlist配置信息
        String[] split = hostlist.split(",");
        //建立HttpHost数组,其中存放es主机和端口的配置信息
        HttpHost[] httpHostArray = new HttpHost[split.length];
        for(int i=0;i<split.length;i++){
            String item = split[i];
            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
        }
        return RestClient.builder(httpHostArray).build();
    }

}

启动类

@EntityScan("com.xuecheng.framework.domain.search")//扫描实体类
@ComponentScan(basePackages={"com.xuecheng.api"})//扫描接口
@ComponentScan(basePackages={"com.xuecheng.search"})//扫描本项目下的全部类
@ComponentScan(basePackages={"com.xuecheng.framework"})//扫描common下的全部类
public class SearchApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SearchApplication.class, args);
    }

} 
相关文章
相关标签/搜索