建立索引:java
PUT /movies/_doc/1 { "name":"The film, filmed in 2021 & tells the story of children" }
按分词搜索:app
GET /movies/_search { "query": { "match": {"name": "story"} } }
经过单个词,能够搜索匹配到结果, 采用analyze查看分词信息:学习
GET /movies/_analyze { "field": "name", "text": "The film, filmed in 2021 & tells the story of children" }
analyze分词处理流程:网站
分词器的使用:ui
若是搜索关键词为tell是没有任何结果, 这个时候须要采用英文分词器。spa
#从新建立索引 PUT /movies { "settings":{ "index":{ "number_of_shards": 1, "number_of_replicas": 0 } }, "mappings": { "properties": { "name":{"type":"text", "analyzer": "english"} } } }
从新插入数据, 采用关键词tell搜索, 能够找到对应的结果:code
"hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.2876821, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "1", "_score" : 0.2876821, "_source" : { "name" : "The film, filmed in 2021 & tells the story of children" } } ] }
经过英文分词器, 会进行词干转化。好比一个单词tells,实际词干为tell, 均可以进行搜索匹配。orm
TMDB是开源的电影网站数据, 里面累积数据较多,比较规范化,便于ES的研究和学习。blog
下载地址索引
导入工程
ESConfig的链接配置类:
@Bean public TransportClient getClient(){ TransportClient transportClient = null; try { Settings settings = Settings.builder().build(); transportClient = new PreBuiltTransportClient(settings); // ES的链接配置信息, 默认transport传输端口为9300,不是9200 TransportAddress firstAddress = new TransportAddress(InetAddress.getByName("10.10.20.28"),Integer.parseInt("9300")); transportClient.addTransportAddress(firstAddress); }catch (Exception e){ e.printStackTrace(); } return transportClient; }
ESController提供导入接口:
@RequestMapping("/importdata") @ResponseBody public ResponseEntity importdata() throws IOException { ... // 索引结构配置信息, 索引名称要配置正确 bulkRequest.add(new IndexRequest("movies", "_doc", String.valueOf(lineId-1)).source(XContentType.JSON, "title", records[17], "tagline",records[16], "release_date",date, "popularity",records[8], "cast",cast, "overview",records[7])); ... }
经过kibana建立索引结构
PUT /movies { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "english" }, "tagline": { "type": "text", "analyzer": "english" }, "release_date": { "type": "date", "format": "8yyyy/MM/dd||yyyy/M/dd||yyyy/MM/d||yyyy/M/d" }, "popularity": { "type": "double" }, "cast": { "type": "object", "properties": { "character": { "type": "text", "analyzer": "standard" }, "name": { "type": "text", "analyzer": "standard" } } }, "overview": { "type": "text", "analyzer": "english" } } } }
调用接口: http://127.0.0.1:8080/es/importdata
会读取csv文件, 自动导入数据。
经过kibana后台,查看导入的数据:
搜索查询
搜索title为heart的关键字
GET /movies/_search { "query":{ "match":{"title":"heart"} } }
可以根据english分词器进行搜索匹配返回全部相关的结果, 每一个结果都会有对应的_score评分,关键字出现频率越高, 或占比越高, 则得分越高,优先排在前面。
or匹配
GET /movies/_search { "query": { "match": { "title": "heart from" } } }
match搜索实质上就是or关系, 分为heart 和from两个关键词进行or关系搜索。
or的最小词匹配控制
GET /movies/_search { "query":{ "match":{ "title": { "query": "good hearts sea", "operator": "or", "minimum_should_match": 2 } } } }
这里minimum_should_match设定为2, 只要出现good hearts 和 hearts sea,都会展现出来。
and匹配
GET /movies/_search { "query":{ "match":{ "title": { "query": "heart sea", "operator": "and" } } } }
经过operator属性来标识对应的操做。这个时候搜索出来的title会包含heart和sea两个关键字。
短语查询
若是想直接搜索某个短语, 好比:The Good Heart, 能够采用match_phrase
GET /movies/_search { "query":{ "match_phrase":{"title":"The Good Heart"} } }
会作整个短语的完整匹配, 不会再进行拆分匹配。
多字段查询
若是想对多个字段同时查询, 能够采用multi_match方式。
GET /movies/_search { "query":{ "multi_match":{ "query": "good hearts sea", "fields": ["title", "overview"] } } }
查询title和overview两个属性, 都包含“good hearts sea”的记录, 相比一个属性title的查询, 多出更多的记录。
能够采用更简便的方式,直接使用AND、OR和NOT操做。
GET /movie/_search { "query":{ "query_string":{ "fields":["title"], "query":"heart AND sea" } } }
查出title当中既包含heart又包含sea的数据。
GET /movie/_search { "query":{ "query_string":{ "fields":["title"], "query":"heart OR sea" } } }
查出title当中包含heart或sea的数据。
GET /movie/_search { "query":{ "query_string":{ "fields":["title"], "query":"heart NOT sea" } } }
查出title当中包含heart但不包含sea的数据。
本文由mirson创做分享, 感谢你们的支持, 但愿对你们有所收获! 入群申请,请加WX号:woodblock99