ChiweenieDijon / noonian-sys

GNU Affero General Public License v3.0
0 stars 0 forks source link

Full text search composites #57

Open ChiweenieDijon opened 6 years ago

ChiweenieDijon commented 6 years ago

full text search should dig into composite subfields

ChiweenieDijon commented 6 years ago
function (searchStr, typeDescMap, FieldTypeService) {

    var retCond = {$or:[
        {__disp:{$regex:searchStr, $options:'i'}},
        {__match_text:{$regex:searchStr, $options:'i'}}
    ]};
    var condList = retCond.$or;

    const processField = function(fieldName, tdMap, prefix) {

        var td = tdMap[fieldName];
        if(fieldName.indexOf('_') === 0)
            return;

        if(td instanceof Array) {
            td = td[0];
        }

        prefix = prefix || '';

        var mongoType = FieldTypeService.getSchemaElem(td);

        if(!mongoType) {
          console.log("No typeMapper for "+td.type+" field "+fieldName);
          return
        }

        var newCond;
        if(mongoType.textIndex) {
            newCond = {};
            newCond[prefix+fieldName] = {$regex:searchStr, $options:'i'};
            condList.push(newCond);
        }
        else if(td.type === 'reference') {
            newCond = {};
            newCond[prefix+fieldName+'._disp'] = {$regex:searchStr, $options:'i'};
            condList.push(newCond);
        }
        else if(td.type === 'composite' && td.type_desc_map) {
            for(var subField in td.type_desc_map) {
                processField(subField, td.type_desc_map, fieldName+'.');
            }
        }

    };

    for(var fieldName in typeDescMap) {
        processField(fieldName, typeDescMap);
    }

    return retCond;
}