elasticsearch. 向导

memcached

memcached模块可以让你在memcached的协议上执行 elasticsearchAPI
The memcached module allows to expose elasticsearch API over the memcached protocol (as closely as possible).

使用方式很简单,只需用下载安装插件 transport-memcached
It is provided as a plugin called transport-memcached and installing is as simply as executing plugin -install transport-memcached or downloading the memcached plugin and placing it under the plugins directory.

memcached同时支持二级制和文本的通讯协议,且会自动选择判断并选择正确的协议。
The memcached protocol supports both the binary and the text protocol, automatically detecting the correct one to use.

memcached协议与ES的REST接口的映射关系
Mapping REST to Memcached Protocol

memcached命令在elasticsearch里面是由同一个REST层来映射和处理的(并不是直接暴露memcached的接口,只是接口方式为memcached的协议),下面是一个支持的memcached命令的列表:
Memcached commands are mapped to REST and handled by the same generic REST layer in elasticsearch. Here is a list of the memcached commands supported:

GET

memcached的 GET 命令对应于REST里面的 GET 。使用URI作为key(注:包含参数的url),因为memcached协议的GET约定get的时候,只能传递一个key的参数,所以在http对应的get或者post里面,如果要传递body参数是不能用的,但是这里我们有一个办法,es支持通过参数"source"来传递body的内容。
The memcached GET command maps to a REST GET. The key used is the URI (with parameters). The main downside is the fact that the memcached GET does not allow body in the request (and SET does not allow to return a result…). For this reason, most REST APIs (like search) allow to accept the “source” as a URI parameter as well.

SET

memcached SET 与REST POST 对应,key值就是url(包含参数),body(value)对应于REST的body。
The memcached SET command maps to a REST POST. The key used is the URI (with parameters), and the body maps to the REST body.

DELETE

memcached DELETE 协议与REST DELETE 对应,key就是URI(包含参数)。
The memcached DELETE command maps to a REST DELETE. The key used is the URI (with parameters).

QUIT

QUIT 支持用来断开与服务器的连接
The memcached QUIT command is supported and disconnects the client.

Settings

有如下对应的设置可配。
The following are the settings the can be configured for memcached:

Setting Description
memcached.port A bind port range. Defaults to 11211-11311.

该组件共享设置 network settings
It also shares the uses the common network settings.

Disable memcached

memcached模块可以通过设置参数 memcached.enabledfalse 来完全停用,默认检测到有插件就启用。
The memcached module can be completely disabled and not started using by setting memcached.enabled to false. By default it is enabled once it is detected as a plugin.


实战补充——————————————————-
1.插件安装,默认memcached插件开启端口11211


cd bin
./plugin install elasticsearch/elasticsearch-transport-memcached/1.1.0
./elasticsearch

2.来个hello world,获取下es版本信息


Xshell:\> telnet localhost 11211

Looking up host ‘localhost’…
Host ‘localhost’ resolved to 127.0.0.1.
Connecting to 127.0.0.1:11211…
Connection established.
Escape character is ‘^@]’.
get /
VALUE / 0 180
{
“ok” : true,
“status” : 200,
“name” : “Rhiannon”,
“version” : {
“number” : “0.19.0”,
“snapshot_build” : false
},
“tagline” : “You Know, for Search”
}
END

3.索引测试文档,注意body内容需要urlencode一下的。
如:

{"name":"medcl"}

urlencode之后为:

%7b%22name%22%3a%22medcl%22%7d

set /index/type/2 %7b%22name%22%3a%22medcl%22%7d

Connection closed by foreign host.

set index/type/2 {"name":"medcl"}

Connection closed by foreign host.

服务器异常了,已提交 issue2 ,等待更新。


[2012-03-27 10:53:52,229][ERROR][memcached.netty ] caught exception on
memcached decoder

4.获取索引文档


get /index/type/1
VALUE /index/type/1 0 100
{"_index":“index”,“_type”:“type”,“_id”:“1”,_version,exists, “_source” : {"name":"medcl"}}
END

5.查询索引


get /index/search?q=name:medcl
VALUE /index/search?q=name:medcl 0 223
{"took":124,timed_out,“_shards”:{"total":2,successful,failed,“hits”:{"total":1,max_score,“hits”:[{"_index":“index”,“_type”:“type”,“_id”:“1”,_score, “_source” : {"name":"medcl"}}]}}
END

QueryDSL方式,查询(注意:需要去掉换行符)

{ “query”: { “bool”: { “should”: [ { "match
all": {} } ] } }, “from”: 0, “size”: 10}

urlencode之后

%7b++++%22query%22%3a+%7b++++++++%22bool%22%3a+%7b++++++++++++%22should%22%3a+%5b++++++++++++++++++++++++++%7b++++++++++++++++++++%22match
all%22%3a+%7b%7d++++++++++++++++%7d++++++++++++%5d++++++++%7d++++%7d%2c++++%22from%22%3a+0%2c++++%22size%22%3a+10%7d

telnet之后执行结果:


get /index/search?source=%7b++++%22query%22%3a+%7b++++++++%22bool%22%3a+%7b++++++++++++%22should%22%3a+%5b++++++++++++++++++++++++++%7b++++++++++++++++++++%22match_all%22%3a+%7b%7d+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%7d++++++++++++%5d++++++++%7d++++%7d%2c++++%22from%22%3a+0%2c++++%22size%22%3a+10%7d
VALUE /index/search?source=%7b++++%22query%22%3a+%7b++++++++%22bool%22%3a+%7b++++++++++++%22should%22%3a+%5b++++++++++++++++++++++++++%7b++++++++++++++++++++%22match_all%22%3a+%7b%7d+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%7d++++++++++++%5d++++++++%7d++++%7d%2c++++%22from%22%3a+0%2c++++%22size%22%3a+10%7d 0 207
{"took":3,timed_out,“_shards”:{"total":2,successful,failed,“hits”:{"total":1,max_score,“hits”:[{"_index":“index”,“_type”:“type”,“_id”:“1”,"
score":1.0, "
source" : {"name":"medcl"}}]}}
END

6.删除索引文档


delete /index/type/1
DELETED

 
Fork me on GitHub