es语法 rest api 模拟根据歌手,歌名,歌词来搜索demo

2024-10-09 08:49:14 浏览数 (3)

#创建索引 songs_v1 PUT { - "acknowledged": true, "shards_acknowledged": true, "index": "songs_v1" }

#创建映射 高版本的没有type,可以使用默认的_doc,避免不同的type之间存在相同的字段名,但是字段类型不同的问题。 { - "error": "no handler found for uri [/songs_v1/mapping/popular] and method [PUT]" }

_doc { - "error": { - "root_cause": [ - { - "type": "illegal_argument_exception", "reason": "Types cannot be provided in put mapping requests" } ], "type": "illegal_argument_exception", "reason": "Types cannot be provided in put mapping requests" }, "status": 400 }

songs_v1/_mapping/ PUT songs_v2/_mappings PUT 也成功 { "properties":{ "songName":{"type":"text"}, "singer":{"type":"text"}, "lyrics":{"type":"text"} } } 返回 { - "acknowledged": true }

浏览器上面访问 http://IP:9200/songs_v1/_settings?pretty http://IP:9200/songs_v1/_mappings?pretty

红色框的{}需要去掉。

索引数据 songs_v2/_doc POST { "songName":"tianyi", "singer":"liudehua", "lyrics":"shui zai hu wo de xin li you duo ku" } 返回 { - "_index": "songs_v2", "_id": "40nNMYEBLnSF9_D_lR0_", "_version": 1, "result": "created", "_shards": { - "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }

查询,根据歌手查询 songs_v2/_search?q=singer:liudehua GET 根据歌词查询 songs_v2/_search?q=lyrics:duo GET 根据歌名来查询 songs_v2/_search?q=songName:wangqingshui

文本匹配不到问题 因ES分词处理导致匹配不到 不需要做模糊查询的字段,使用keyword代替text,避免创建索引的时候对这些词进行分词。

查询全部 songs_v2/_search { "query": { "match_all": { "boost": 1 } } } 根据歌名查询 songs_v2/_search { "from": 0, "size": 5, "timeout": "60s", "query": { "term": { "songName": { "value": "tianyi", "boost": 1 } } } }

0 人点赞