ElasticSearch不同类型下同名字段排序错误
小异常,记录一把。
Write down, forget!
小异常,记录一把。
大家在写QueryDSL的时候,要特别注意参数,比如今天我就碰到了一个关于_cache的问题 由于使用自己写的QueryBuilder来生成QueryDSL查询语句,所以有些参数虽然没有指定,但是默认带上了,但有些情况下不注意就会出现问题。 如下例:
cn域名解锁就是慢啊, 以后ES中文文档都放这里了:http://doc.elasticsearch.cn 下面是已完成的部分链接: 安装:http://doc.elasticsearch.cn/guide/reference/setup/ 配置:http://doc.elasticsearch.cn/guide/reference/setup/configuration.html API接口说明:http://doc.elasticsearch.cn/guide/reference/api/ QueryDSL查询:http://doc.elasticsearch.cn/guide/reference/query-dsl/ Mapping:http://doc.elasticsearch.cn/guide/reference/mapping/ 索引模块:http://doc.elasticsearch.cn/guide/reference/index-modules/store.html 事务日志:http://doc.elasticsearch.cn/guide/reference/index-modules/translog.html … 文档比较多,招志愿者一起翻译。
https://github.com/mojombo/jekyll/wiki/Deployment jekyll&es-doc部署脚本
elasticsearch里面的search_type共有如下几种: The type of the search operation to perform. Can be dfs_query_then_fetch, dfs_query_and_fetch, query_then_fetch, query_and_fetch. 【removed,since:http://groups.google.com/group/elasticsearch/browse_thread/thread/7aa5ea823afb499/d9e3cf3a1e1f6964】 Defaults to query_then_fetch. form google group:“ You get proper sorted results when you use query_then_fetch (across all top “size” results), if you use query_and_fetch, […]
新建了2个qq群,欢迎大家一起交流elasticsearch方面的相关内容。 群1:190605846(已满) 群2:211682609(欢迎)2012/05/28更新 另外,已经刚申请了elasticsearch.cn域名,打算组建一个elasticsearch在国内的交流社区,整理收集相关的资料文档,方便新手学习elasticsearch和促进elasticsearch在国内的推广。 第一步,打算先将官方的站点的文档翻译下,毕竟目前还没有比那更完善的文档了,由于文档比较多,所以在这里希望能招募有共同想法的童鞋一起来完成这项伟大的工作。 有什么想法请留意或加QQ群吧。 you know for search , :)
前面一篇介绍parent-child的使用,我们来回顾一下: 1.先建好mapping和索引几条数据
1 2 3 4 5 |
curl -XPUT 'http://localhost:9200/news/comment/_mapping' -d '{ "comment" : { "_parent" : { "type" : "hot" } }}' curl -XPUT 'http://localhost:9200/news/comment/1?parent=1' -d '{ "uname" : "凤凰网安徽省六安市网友", "content" : "河南警方的话没人信"}' curl -XPUT 'http://localhost:9200/news/comment/2?parent=1' -d '{ "uname" : "凤凰网湖北省武汉市网友", "content" : "没有监督权\n没有调查\n一切当然只能是谣言"}' curl -XPUT 'http://localhost:9200/news/comment/3?parent=1' -d '{ "uname" : "ladygaga", "content" : "双下肢不活动,存在废用性肌肉萎缩。请问,这个是怎么做到的?"}' curl -XPUT 'http://localhost:9200/news/comment/4?parent=1' -d '{ "uname" : "medcl", "content" : "额"}' |
2.获取一下这几条数据看看
1 |
http://localhost:9200/news/comment/1 |
结果:
1 |
{"_index":"news","_type":"comment","_id":"1","_version":1, "_source" : { "uname" : "凤凰网安徽省六安市网友", "content" : "河南警方的话没人信"}} |
没有问题,我们再试试后面的
1 |
http://localhost:9200/news/comment/2 |
结果:
1 2 3 4 5 6 |
HTTP/1.1 404 Not Found Access-Control-Allow-Origin: * Content-Type: application/json; charset=UTF-8 Content-Length: 45 {"_index":"news","_type":"comment","_id":"2"} |
嘿嘿,发现了么,居然是404,你可以继续试试后面的id为3的也是404,id为4的可以出来 试试:http://localhost:9200/news/comment/2?parent=1(索引时的path)
1 |
HTTP/1.1 404 Not Found |
哈哈,貌似不行o.(ps:其实kimchy可以实现这个url pattern,但是目前没有) 那正确的方式是怎样的呢?
1 2 3 4 |
http://localhost:9200/news/comment/1?routing=1 http://localhost:9200/news/comment/1?routing=2 http://localhost:9200/news/comment/1?routing=3 http://localhost:9200/news/comment/1?routing=4 |
答案就在routing,ES帮助:http://www.elasticsearch.org/guide/reference/mapping/routing-field.html 使用我写的partial_update插件也是支持routing的,如下:
1 |
curl -XPUT 'http://localhost:9200/news/comment/4/_update?parent=1' -d '{ "content" : "连老卡都不斗争了,难道真的登船去了吗?"}' |
结果:
1 2 |
curl -XGET http://localhost:9200/news/comment/4?routing=1 {"_index":"news","_type":"comment","_id":"4","_version":2, "_source" : {"content":"连老卡都不斗争了,难道真的登船去了吗?","uname":"medcl"}} |
发散一下,parent=2试试:
1 |
curl -XPUT 'http://localhost:9200/news/comment/4/_update?parent=2' -d '{ "content" : "周公使管叔监殷"}' |
结果:
1 2 3 4 5 |
curl -XGET http://localhost:9200/news/comment/4?routing=2 {"_index":"news","_type":"comment","_id":"4","_version":1, "_source" : {"content":"周公使管叔监殷","uname":"medcl"}} curl -XGET http://localhost:9200/news/comment/4?routing=1 {"_index":"news","_type":"comment","_id":"4","_version":2, "_source" : {"content":"连老卡都不斗争了,难道真的登船去了吗?","uname":"medcl"}} |
很明细,/news/comment/4存在两条记录,routing的出现,使ES的id的唯一性丢失了,并且删除索引记录的时候也必须带上routing才行,此外,查询的结果中可能会出现重复的_id。
1 2 |
curl -XDELETE http://localhost:9200/news/comment/4?routing=1 curl -XDELETE http://localhost:9200/news/comment/4?routing=2 |
再看看查询的操作,查询的时候可以指定routing,默认不区分routing,即全部扫描:
1 2 |
curl -XGET http://localhost:9200/news/comment/_search?q=* curl -XGET http://localhost:9200/news/comment/_search?q=*&routing=3 |
总之,一旦你决定使用routing,你必须保证对这些routing做到心中有数。 补充一下: 什么是routing,为什么用routing,正常情况下,索引是根据type和id通过hash取模的方式来存储到不同的shard里面的,查询的时候则是在整个shard组里面做的,即每个shard都要参与查询,然后合并各个查询结果,想想,如果shard多了之后,其实有些shard里面可能根本就没有我们需要的数据,这样就浪费了很多不必要的查询操作,routing就是可以按照一定的规则,建索引的时候,就可以指定数据存放在哪个shard里面,这样查询的时候,同理,通过routing规则就能够保证有的放矢,只在一个shard里面去进行查询,而不是到处撒网,这样不就快多了吗?当然用routing也有缺点,由于索引存放位置由我们自己控制,并且由于routing值不均匀,肯定会造成索引数据不均匀,即某几个shard里面什么数据也没有,某几个shard里面数据扎堆,数据扎堆的shard肯定对性能有影响,so,怎么用,自己决定!
介绍下ElasticSearch里Parent-Child特性的使用。 //首先创建一系列新闻的索引,这里我们将hot类型作为parent-chid关系里面的parent。
1 2 3 |
curl -XPUT 'http://localhost:9200/news/hot/1' -d '{ "uname" : "medcl", "content" : "河南警方:“南阳老板遭逼供致残与狗同笼”纯属谎言"}' curl -XPUT 'http://localhost:9200/news/hot/2' -d '{ "uname" : "medcl", "content" : "马英九打两岸牌反制绿营"}' curl -XPUT 'http://localhost:9200/news/hot/3' -d '{ "uname" : "medcl", "content" : "专题:中共十七届六中全会公报"}' |
前面应该介绍过ES是Schema Free,但是Schema Free不是说没有Schema,和Solr一样,ElasticSearch也可以设置document的schema,ES里的名字叫Mapping,其实无非就是设置document包含哪些Field,然后对每一个Field个性化的设置索引类型,是否存储,以及设置索引分析器和查询使用的分析器,Es和Solr相比有一个我认为最好的特性:就是支持Object类型,你可以像操作对象一样对对象的某个属性进行索引和查询,简单演示如下: