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

Copyright © 2017 kaka