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);
    }

} 
相關文章
相關標籤/搜索