1

i'm using elasticsearch-rails for a project, there is a combine search feature, all columns are in one table. I just write a custom search function, and the search dsl didn't work, can't have any results.

def self.combine_search_filter(remark=nil, sim_card_supplier_id=nil, work_mode=nil, operator_status=nil, platform_management_status=nil, online_status=nil, sim_card_set_id=nil, iccid_from=nil, iccid_to=nil, actived_at_from=nil, actived_at_to=nil, check_in_at_from=nil, check_in_at_to=nil, device_mac_from=nil, device_mac_to=nil)
    response = __elasticsearch__.search(
      "size": 1000,
      "query": {
        "filtered": {
          "filter":   {
            "bool": {
              "filter":   [
                { "term":  { "sim_card_supplier_id": sim_card_supplier_id } },
                { "term":  { "work_mode": work_mode } },
                { "term":  { "operator_status": operator_status } },
                { "term":  { "platform_management_status": platform_management_status } },
                { "term":  { "online_status": online_status } },
                { "term":  { "sim_card_set_id": sim_card_supplier_id } }
                { "range": { "iccid": { "from": iccid_from, "to": iccid_to }}},
                { "range": { "check_in_at": { "from": check_in_at_from, "to": check_in_at_to }}},
                { "range": { "actived_at": { "from": actived_at_from, "to": actived_at_to }}},
                { "range": { "device_mac": { "from": device_mac_from, "to": device_mac_to }}}
              ]
            }
          }
        }
      }
    )
end

and the params maybe pass nil, how can i do that make the search dsl valid?

JCJC
  • 35
  • 6
  • I have no experience with `elasticsearch-rails` gem but I am pretty sure this can be easily done using `searchkick` https://github.com/ankane/searchkick which again uses elasticsearch server. give it shot if you like. – Abhinay Dec 08 '16 at 04:29
  • Does it throws any error? can you post that as well? – Abhinay Dec 08 '16 at 04:41

2 Answers2

1

I would rather suggest you to use searchkick gem for rails which makes elastic search as simple as that and makes your search more intelligent and queries simple.

0

My bad, i just figure this out, ruby keyword function should be like this:

def self.combine_search_filter(options = {})
    response = __elasticsearch__.search(
      "size": 1000,
      "query": {
        "filtered": {
          "filter":   {
            "bool": {
              "filter":   [
                { "term":  { "sim_card_supplier_id": 104 } },
                { "term":  { "work_mode": options[:work_mode] } },
                { "term":  { "operator_status": options[:operator_status] } },
                { "term":  { "platform_management_status": options[:platform_management_status] } },
                { "term":  { "online_status": options[:online_status] } },
                { "term":  { "sim_card_set_id": 76 } },
                { "range": { "iccid": { "from": options[:iccid_from], "to": options[:iccid_to] }}},
                { "range": { "check_in_at": { "from": options[:check_in_at_from], "to": options[:check_in_at_to] }}},
                { "range": { "actived_at": { "from": options[:actived_at_from], "to": options[:actived_at_to] }}},
                { "range": { "device_mac": { "from": options[:device_mac_from], "to": options[:device_mac_to] }}}
              ]
            }
          }
        }
      }
    )
end

this search will have the correct result. The options[:key] may be nil will cause the wrong result, so i'm gonna delete the nil key of the dsl hash, then pass it to search()

JCJC
  • 35
  • 6