在Windows下使用Curl工具完美操作ElasticSearch

来源:互联网 发布:罗斯福连任 知乎 编辑:程序博客网 时间:2024/06/05 07:27
首先可以从 http://curl.haxx.se/download.html上下载并安装Windows环境下的Curl。安装完成后,将Curl的I386目录的完整路径添加到Windows的Path环境变量中。
C:\Users\Carl>curl -XGET http://localhost:9200/{  "name" : "node_01",  "cluster_name" : "dfs",  "version" : {    "number" : "2.4.0",    "build_hash" : "ce9f0c7394dee074091dd1bc4e9469251181fc55",    "build_timestamp" : "2016-08-29T09:14:17Z",    "build_snapshot" : false,    "lucene_version" : "5.5.2"  },  "tagline" : "You Know, for Search"}
但是,Windows下的Curl不支持多行输入,搜索之后,http://stackoverflow.com/questions/3007253/send-post-xml-file-using-curl-command-line上提供的方法也不灵。在Curl每行命令最后加上^符号,确实可以输入完成整个命令,但不能获得正常结果:
C:\Users\Carl>curl -XPUT http://localhost:9200/dept/employee/2?pretty -d '^More? {^More? "empname": "Carl Wu2"^{  "error" : {    "root_cause" : [ {      "type" : "mapper_parsing_exception",      "reason" : "failed to parse"    } ],    "type" : "mapper_parsing_exception",    "reason" : "failed to parse",    "caused_by" : {      "type" : "not_x_content_exception",      "reason" : "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"    }  },  "status" : 400}curl: (6) Could not resolve host: Carl Wu2^

一番搜索后,终于发现Cygwin和Curl联手,可以完美解决这个问题。我们可以从http://cygwin.com/install.html上下载安装,然后进入CygWin命令行,键入:
curl -XPUT http://localhost:9200/dept/employee/2?pretty -d '{"empname": "Carl Wu2"}'

返回结果如下所示:


再举个例子。我们可以使用下面的命令创建一个名叫my_index的索引,type名为blog,blog下面有一个属性叫title,该属性的分析器(Analyzer)为缺省的标准分词器(standard),title下面有个title.english的子属性,用的是english分词器。

curl -XPUT "http://localhost:9200/my_index" -d'{  "mappings": {    "blog": {      "properties": {        "title": {           "type": "string",          "fields": {            "english": {               "type":     "string",              "analyzer": "english"            }          }        }      }    }  }}'


我们可以试着插入两条数据,请注意I'm happy被转换为I\"m happy才可在curl下正常执行。

curl -XPUT "http://localhost:9200/my_index/blog/1" -d'{ "title": "I\"m happy for this fox" }'curl -XPUT "http://localhost:9200/my_index/blog/2" -d'{ "title": "I\"m not happy about my fox problem" }'

然后即可执行下面的搜索语句,其中的搜索类型为multi_match,就是要匹配多个字段。

curl -XGET http://localhost:9200/_search?pretty -d '{  "query": {    "multi_match": {      "type":     "most_fields",       "query":    "not happy foxes",      "fields": [ "title", "title.english" ]    }  }}'

搜索结果如下:

{  "took" : 15,  "timed_out" : false,  "_shards" : {    "total" : 30,    "successful" : 30,    "failed" : 0  },  "hits" : {    "total" : 2,    "max_score" : 0.09362626,    "hits" : [ {      "_index" : "my_index",      "_type" : "blog",      "_id" : "2",      "_score" : 0.09362626,      "_source" : {        "title" : "I\"m not happy about my fox problem"      }    }, {      "_index" : "my_index",      "_type" : "blog",      "_id" : "1",      "_score" : 0.0701148,      "_source" : {        "title" : "I\"m happy for this fox"      }    } ]  }}

请注意搜索关键词中含有foxes这个复数名词。虽然这两个搜索结果中都没有出现这个词,但我们在title.english子属性中定义的english分词器将foxes转换为词根fox,所以我们能够发现这两条记录。另外,由于第二条记录中含有not关键字,title属性中的standard分词器将发挥作用,所以第二条记录被提到第一条的前面。



0 0