云搜索服务 CSS-使用示例:步骤二:使用关键词搜索
步骤二:使用关键词搜索
Elasticsearch 7.x之前的版本和之后的版本,命令有差别,所以分开举例。
- 7.x之前的版本
- 创建索引“book”,配置分词策略。
示例中“analyzer”和“search_analyzer”可以根据实际需要“ik_max_word”或“ik_smart”分词策略,此处以“ik_max_word”为例。
PUT /book{ "settings": { "number_of_shards": 2, "number_of_replicas": 1 }, "mappings": { "type1": { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } }}
- 导入数据,将文本信息导入“book”索引中。
PUT /book/type1/1{ "content":"智能手机是很好用"}
- 使用关键词“智能手机”进行文本搜索,并查看搜索结果。
GET /book/type1/_search{ "query": { "match": { "content": "智能手机" } }}
搜索结果:
{ "took" : 20, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.1507283, "hits" : [ { "_index" : "book", "_type" : "type1", "_id" : "1", "_score" : 1.1507283, "_source" : { "content" : "智能手机是很好用" } } ] }}
- 创建索引“book”,配置分词策略。
- 7.x及之后的版本
- 创建索引“book”,配置分词策略。
示例中“analyzer”和“search_analyzer”可以根据实际需要“ik_max_word”或“ik_smart”分词策略,此处以“ik_max_word”为例。
PUT /book{ "settings": { "number_of_shards": 2, "number_of_replicas": 1 }, "mappings": { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } }}
- 导入数据,将文本信息导入“book”索引中。
PUT /book/_doc/1 { "content":"智能手机是很好用" }
- 使用关键词“智能手机”进行文本搜索,并查看搜索结果。
GET /book/_doc/_search{ "query": { "match": { "content": "智能手机" } }}
搜索结果:
{ "took" : 16, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.7260926, "hits" : [ { "_index" : "book", "_type" : "_doc", "_id" : "1", "_score" : 1.7260926, "_source" : { "content" : "智能手机是很好用" } } ] }}
- 创建索引“book”,配置分词策略。