【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

相关