【Elasticsearch/CURL】CURL访问ES常用语句之二--全文匹配查找
【添加测试数据】
curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/ibm/emp/1?pretty' -d' {"name":"andy","age":"25","resume":"graduated from tsinghua,joined microsoft in 2002.1.1,quit google in 2003.1.1"}' curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/ibm/emp/2?pretty' -d' {"name":"bill","age":"35","resume":"graduated from baida,joined baidu in 2002.1.1,joined ali in 2003.1.1"}' curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/ibm/emp/3?pretty' -d' {"name":"cindy","age":"45","resume":"graduated from renda,joined tencent in 2002.1.1,joined baidu in 2003.1.1"}' curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/ibm/emp/4?pretty' -d' {"name":"douglas","age":"55","resume":"graduated from beiligong,joined ali in 2002.1.1,refused microsoft in 2003.1.1"}'
【需要】
从IBM的雇员记录中,找出曾加入微软(joined microsoft)的员工。
【从resume字段中找存在microsoft关键字的】
命令:
curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/ibm/emp/_search?pretty' -d' { "query":{ "match":{ "resume":"microsoft" } } }'
反馈:
{ "took" : 383, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.6931471, "hits" : [ { "_index" : "ibm", "_type" : "emp", "_id" : "1", "_score" : 0.6931471, "_source" : { "name" : "andy", "age" : "25", "resume" : "graduated from tsinghua,joined microsoft in 2002.1.1,quit google in 2003.1.1" } }, { "_index" : "ibm", "_type" : "emp", "_id" : "4", "_score" : 0.6931471, "_source" : { "name" : "douglas", "age" : "55", "resume" : "graduated from beiligong,joined ali in 2002.1.1,refused microsoft in 2003.1.1" } } ] } }
这里把加入微软和拒绝微软都找出来了,后面明显不是预期的加入微软。
【从resume字段中找存在joined或microsot关键字的】
命令:
curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/ibm/emp/_search?pretty' -d' { "query":{ "match":{ "resume":"joined microsoft" } } }'
反馈:
{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.79850763, "hits" : [ { "_index" : "ibm", "_type" : "emp", "_id" : "1", "_score" : 0.79850763, "_source" : { "name" : "andy", "age" : "25", "resume" : "graduated from tsinghua,joined microsoft in 2002.1.1,quit google in 2003.1.1" } }, { "_index" : "ibm", "_type" : "emp", "_id" : "4", "_score" : 0.79850763, "_source" : { "name" : "douglas", "age" : "55", "resume" : "graduated from beiligong,joined ali in 2002.1.1,refused microsoft in 2003.1.1" } }, { "_index" : "ibm", "_type" : "emp", "_id" : "2", "_score" : 0.1448707, "_source" : { "name" : "bill", "age" : "35", "resume" : "graduated from baida,joined baidu in 2002.1.1,joined ali in 2003.1.1" } }, { "_index" : "ibm", "_type" : "emp", "_id" : "3", "_score" : 0.1448707, "_source" : { "name" : "cindy", "age" : "45", "resume" : "graduated from renda,joined tencent in 2002.1.1,joined baidu in 2003.1.1" } } ] } }
这里把加入和微软分开了查找,排名最高的可能是我们想要的结果。
【从resume字段中找存在joined microsoft短语的,该短语不进行拆分】
命令:
curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/ibm/emp/_search?pretty' -d' { "query":{ "match_phrase":{ "resume":"joined microsoft" } } }'
反馈:
{ "took" : 37, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.7985077, "hits" : [ { "_index" : "ibm", "_type" : "emp", "_id" : "1", "_score" : 0.7985077, "_source" : { "name" : "andy", "age" : "25", "resume" : "graduated from tsinghua,joined microsoft in 2002.1.1,quit google in 2003.1.1" } } ] } }
最终精确找出了曾经加入微软的员工。
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2018-04-17 【Canvas与图标】绿色数据库图标