0

Is there any way that we can convert data type of a field while querying using filters. The scenario that I had is I have a document with a field

customData: {
    "contactCustomFieldMapId":xxxx,
    "contactId":xxxxx,
    "customFieldId":45788,
    "value":"1899",
    "fieldInputTypeId":0
},
{
    "contactCustomFieldMapId":xxxx,
    "contactId":xxxxx,
    "customFieldId":45732,
    "value":"Meet me at noon",
    "fieldInputTypeId":0
},
{
    "contactCustomFieldMapId":xxxx,
    "contactId":xxxxx,
    "customFieldId":23233,
    "value":"233589",
    "fieldInputTypeId":0
}

In the above field the value property can be of any datatype(string, datatime, or number), where as I need to fetch data using range filters (greater than, less than). This range filter should be combined with a term filter I made a query using bool filter as

"filter": {
"and": {
  "filters": [
    {
      "bool": {
        "must": [
          {
            "and": {
              "filters": [
                {
                  "term": {
                    "customData.customFieldId": 45788
                  }
                },
                {
                  "range": {
                    "customData.value": {
                      "gt": "1000"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

}

Unfortunately the query is fetching all the records with customData.customFieldId as 45788 (working similarly as exist filter).

Is there any way that I can combine both the term filter and range filter.

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
AR M
  • 303
  • 3
  • 7

1 Answers1

0

maybe the query is not well formated. i use this :

 {
   "filtered": {
     "filter": {
       "and": [
         { "term": {
                  "customData.customFieldId": 45788
              }
         },
         { "range": {
                 "customData.value": {
                       "gt": "1000"
                     }
               }
         }
       ]
     }
   }
  }
AlainIb
  • 4,544
  • 4
  • 38
  • 64
  • No change, bringing all the records. Is this issue related to filed type of `customData.value` – AR M Apr 03 '15 at 08:29
  • I don't know sorry. maybe you can try to make a mapping of your data before making the index. the mapping let you choose a type for each field ( and format like uppercase/lowercase/ to analyse the data ect ) – AlainIb Apr 03 '15 at 11:54