git
Comments

Git 错误 Object Is Empy / Corrupt

问题

早上上班开始工作是准备git push到远程服务器时,出现下面错误!我估计是之前的merge导致的

1
2
  error: object file .git/objects/xx/xxx is empty
  fatal: loose object xxx (stored in .git/objects/xx/xxx is corrupt

解决方案

参考链接, 其实我就是删除空的commit就可以了

find .git/objects/ -size 0 -exec rm -f {} \; 
Comments

Elasticsearch 动态加载属性的mapping

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  curl -XPUT 'localhost:9200/products' -d '{
    "settings": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin",
            "filter": ["standard", "nGram"]
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type": "pinyin",
            "first_letter": "prefix",
            "padding_char": ""
          }
        }
      }
    },
    "mappings": {
      "product": {
        "dynamic_templates": [
          {
            "property_template": {
              "path_match": "properties.*",
              "mapping": {
                "type": "multi_field",
                "fields": {
                  "{name}": {
                    "type": "string",
                    "analyzer" : "pinyin_analyzer",
                    "boost": 10,
                    "store": "no",
                    "term_vector": "with_positions_offsets"
                  },
                  "{name}_untouched": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          }
        ]
      }
    }
  }'

  curl -XPOST 'localhost:9200/products/product' -d '{name: "加多宝", properties: {ping: 1} }'
  curl -XPOST 'localhost:9200/products/product' -d '{name: "水杯", properties: {ge: 1} }'

  curl -XGET 'localhost:9200/products/product/_mapping?pretty'
Comments

Elasticsearch脚本排序

由于自己要自定义score, 所以才会使用script来写复杂的sort

javascript script 排序

1
bin/plugin -install elasticsearch/elasticsearch-lang-javascript/1.4.0
  • 写js脚本

在你的配置目录下新建文件scripts/products/sort.js, 我的配置目录是/etc/elasticsearch,简单实例:

1
2
3
(function(){
  doc._score.value + doc.likes.value
})()
  • 查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
curl -XGET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "global_product",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

products_sort: products是目录sort脚本文件名

native script 排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -X GET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "productSort",
      "lang": "native",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

js与native排序,性能方面有差距同样的5W数据,比较一下查询速度: js:70ms, native: 20ms

Comments

Elasticsearch 多个索引查询,根据不同类型条件判定

CURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
curl -XGET 'http://localhost:9200/indexA,indexB/_search?pretty=1' -d '{
"query" : {
    "bool": {
      "should": [{
        "filtered": {
          "filter":{
            "and": [
              {
                "range":{
                  "start_time":{
                    "lte": "2013-09-24T00:00:00+08:00"
                  }
                }
              },{
                "range": {
                  "end_time": {
                    "gt": "2013-09-24T00:00:00+08:00"
                  }
                }
              },
              {
                "term": {
                  "_type": "TypeA"
                }
              }]
          }
        }
      },{
        "filtered": {
          "filter": {
            "term": {
              "_type": "TypeB"
            }
          }
        }
      }]
    }
  },
  "sort": [
    {
      "_script": {
        "script": "doc[\u0027score\u0027].value/((time()-doc[\u0027start_time_ms\u0027].value) / 3600)",
        "type": "number",
        "order": "desc"
      },
      "_score": {

      }
    }
  ]
}'

indexA 与 indexB 索引的 TypeA与TypeB

ruby gem Tire

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Tire.search ['indexA', 'indexB'] do
  from _from
  size _size

  query do
    boolean do
      should do
        filtered do
          filter :range, :end_time => {gt: toDay}
          filter :range, :start_time => {lte: toDay}
          filter :term, :_type => "TypeA"
          filter :term, :status => 1
          filter :terms, {"category.id" => category_ids} if q[:catalog_id].present?
        end
      end
      should do
        filtered do
          filter :term, {:_type => "TypeB"}
        end
      end

    end
  end

  sort("_script" => {
    :script => "doc['score'].value/((time()-doc['start_time_ms'].value) / 3600)",
    :type   => "number",
    :order  => "desc"
  }, "_score" => {})

end
Comments

Gem Requirejs-rails Assets Precompile 简介

安装requirejs-rails, 然后新建配置文件config/requirejs.yml

requirejs.yml

1
2
3
4
5
6
7
8
9
10
#配置单个模块,会编译一个单独的模块
modules:
  - name: 'mytoplevel'

#路径
paths
  jquery: "lib/jquery.js"

#设置依赖关系
shim:

其实与requirejs配置差不多,只是rails会根据requirejs.yml编译

Comments

Ubuntu Rabbitmq 服务器安装过程

rabbitmq是用于消息列队处理的,准备研究一下它来做消息处理 rabbitmq是用Erlang开发的,所有安装之前要部署Erlang环境

Erlang部署

wget https://elearning.erlang-solutions.com/binaries/sources/otp_src_R16B.tar.gz
tar xvzf otp_src_R16B.tar.gz
cd otp_src_R16B
./configure
make && make install

Rabbitmq部署

官方安装文档

  1. 添加公共key

    wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc

    sudo apt-key add rabbitmq-signing-key-public.asc

  2. 安装

    sudo apt-get install rabbitmq-server

官方配置文档配置文件路径在/etc/rabbitmq/

启动rabbitmq_management管理

cd /usr/lib/rabbitmq/lib/rabbitmq_server-2.7.1/sbin
sudo ./rabbitmq-plugins enable rabbitmq_management

然后浏览地址http://server-name:55672/, 官方说3.0以下版本端口是55672

Comments

postMessage 跨域通讯

不相同的域可以相互通讯, 例如iframe与页面进行通讯, postMessage是个封装不错的包

简单实例解释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  //在iframe 绑定频道
  pm.bind("message1", function(data) {
    $(document.body).append(JSON.stringify(data));
  });

  //发送消息
  pm({
    //给那个iframe
    target: window.frames["example1"],
    //频道
    type:"message1",
    //内容
    data:{hello:"world"}
  });
Comments

Requirejs 使用

requirejs是一个管理js模块加载与依赖包,可以不使用script标签来加载js文件
1.引用requirejs文件

1
<script data-main="scripts/main" src="scripts/require.js"></script>

2.配置, 更多配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
requirejs.config({

    //默认加载模块路径
    baseUrl: "js/lib"

    //指定模块的路径
    paths:{
        //前面别名, 后面路径
        "jquery" : "js/core/jquery.2.0.js",
        "backbone" : "js/core/backbone0.9.js",
        "underscore": "js/core/underscore.js"
    }

    //设置依赖关系
    shim:{
        backbone:{
            deps:["underscore", "jquery"],
            //可以使用全局别名
            exports: "Backbone"
        }
    }
})

3.使用

1
2
3
4
5
6
7
8
9
10
11
12
13
// js/dom.js
define(["backbone"], function(Backbone){
    LoginView = Backbone.View.extend({
        ...
    });
    return LoginView
})

require(["js/dom"], function(LoginView){
    new LoginView(
        ...
    )
})
Comments

backbone升级0.9.9版本,添加两个方法listenTo与stopListening

listenTo用于绑定事件,stopListening关闭绑定事件了
它们与on有什么不同的呢!这里做个例子

  var Todo = Backbone.Model.extend({
    ...
  })
   
  var todo = new Todo()
  var appView = Backbone.View.extend({
    initialize: function(){
      this.listenTo(todo, "change:name", this.edit_attr)
    },
    edit_attr: function(){
      this.$(".name").html(this.model.get('name'))
    }
  })

  var app_view = new appView()
  app_view.remove()  

当我们app_view.remove()删除之后会自动执行stopListening方法关注绑定事件
todo模型就不会有这个视图绑的事件了,如果

  this.listenTo(todo, "change:name", this.edit_attr)

改用这种绑定方式

  todo.on("change:name", this.edit_attr, this)

app_view.remove()删除之后,todo模型还是会有这个绑定的事件
当然也可以用off来处理,但是现在有listenTo就不用做特别的处理了。

Copyright © 2014 kaka