Queries for db search changed. Needs testing.

This commit is contained in:
Naida Vatric
2019-12-14 01:10:48 +01:00
parent e83712fb33
commit 5a2fdb7291
2 changed files with 137 additions and 97 deletions

View File

@@ -88,7 +88,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
adType, adType,
realEstateType, realEstateType,
areaToSearch, areaToSearch,
//
gardenSizeMin, gardenSizeMin,
gardenSizeMax, gardenSizeMax,
numberOfRoomsMin, numberOfRoomsMin,
@@ -102,7 +101,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
elevator, elevator,
newBuilding, newBuilding,
accessRoadType accessRoadType
//
} = searchRequest; } = searchRequest;
const longitudeColumn = sequelize.col("locationLong"); const longitudeColumn = sequelize.col("locationLong");
@@ -131,50 +129,28 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
const geoSearchQueryPart = sequelize.where(contains, true); const geoSearchQueryPart = sequelize.where(contains, true);
//General queries contain only attributes that are defined for every searchreq
//Query for case of complete ads //Query for case of complete ads
//WIP This wont work, need to separate queries by adType and realEstateType
//because for example flat does not have gardenSize and that can't be in query
const query = { const query = {
adType, adType,
realEstateType, realEstateType,
price: { price: {
[Op.lte]: priceMax, [Op.lte]: priceMax,
[Op.gte]: priceMin [Op.gte]: priceMin
}, },
area: { area: {
[Op.lte]: sizeMax, [Op.lte]: sizeMax,
[Op.gte]: sizeMin [Op.gte]: sizeMin
}, },
[Op.and]: geoSearchQueryPart, accessRoadType: {
// [Op.or]: {
gardenSize: { [Op.eq]: 'ANY',
[Op.lte]: gardenSizeMax, [Op.eq]: accessRoadType
[Op.gte]: gardenSizeMin }
}, },
numberOfRooms: { [Op.and]: geoSearchQueryPart
[Op.lte]: numberOfRoomsMax, }
[Op.gte]: numberOfRoomsMin
},
numberOfFloors: {
[Op.lte]: numberOfFloorsMax,
[Op.gte]: numberOfFloorsMin
},
floor: {
[Op.lte]: floorMax,
[Op.gte]: floorMin
},
accessRoadType: {
[Op.or]: {
[Op.eq]: 'ANY',
[Op.eq]: accessRoadType
}
},
balcony,
newBuilding,
elevator
};
//Query for case of incomplete ads //Query for case of incomplete ads
const queryIncludeIncomplete = { const queryIncludeIncomplete = {
@@ -198,44 +174,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
[Op.is]: null [Op.is]: null
} }
}, },
[Op.and]: geoSearchQueryPart,
//
gardenSize: {
[Op.or] : {
[Op.and]: {
[Op.lte]: gardenSizeMax,
[Op.gte]: gardenSizeMin
},
[Op.is]: null
}
},
numberOfRooms: {
[Op.or]: {
[Op.and]: {
[Op.lte]: numberOfRoomsMax,
[Op.gte]: numberOfRoomsMin
},
[Op.is]: null
}
},
numberOfFloors: {
[Op.or] : {
[Op.and]: {
[Op.lte]: numberOfFloorsMax,
[Op.gte]: numberOfFloorsMin
},
[Op.is]: null
}
},
floor: {
[Op.or] : {
[Op.and]: {
[Op.lte]: floorMax,
[Op.gte]: floorMin
},
[Op.is]: null
}
},
accessRoadType: { accessRoadType: {
[Op.or]: { [Op.or]: {
[Op.eq]: 'ANY', [Op.eq]: 'ANY',
@@ -243,25 +181,109 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
[Op.is]: null [Op.is]: null
} }
}, },
balcony: { [Op.and]: geoSearchQueryPart
}
//Every other attribute is checked separately and included in query only if it is defined
if (gardenSizeMax && gardenSizeMin) {
query.gardenSize = {
[Op.lte]: gardenSizeMax,
[Op.gte]: gardenSizeMin
}
queryIncludeIncomplete.gardenSize = {
[Op.or] : {
[Op.and]: {
[Op.lte]: gardenSizeMax,
[Op.gte]: gardenSizeMin
},
[Op.is]: null
}
}
}
if (numberOfRoomsMin && numberOfRoomsMax) {
query.numberOfRooms = {
[Op.lte]: numberOfRoomsMax,
[Op.gte]: numberOfRoomsMin
}
queryIncludeIncomplete.numberOfRooms = {
[Op.or] : {
[Op.and]: {
[Op.lte]: numberOfRoomsMax,
[Op.gte]: numberOfRoomsMin
},
[Op.is]: null
}
}
}
if (numberOfFloorsMin && numberOfFloorsMax) {
query.numberOfFloors = {
[Op.lte]: numberOfFloorsMax,
[Op.gte]: numberOfFloorsMin
}
queryIncludeIncomplete.numberOfFloors = {
[Op.or] : {
[Op.and]: {
[Op.lte]: numberOfFloorsMax,
[Op.gte]: numberOfFloorsMin
},
[Op.is]: null
}
}
}
if (floorMin && floorMax) {
query.floor = {
[Op.lte]: floorMax,
[Op.gte]: floorMin
}
queryIncludeIncomplete.floor = {
[Op.or] : {
[Op.and]: {
[Op.lte]: floorMax,
[Op.gte]: floorMin
},
[Op.is]: null
}
}
}
if (balcony) {
query.balcony = {
[Op.eq]: balcony
}
queryIncludeIncomplete.balcony = {
[Op.or]: { [Op.or]: {
[Op.eq]: balcony, [Op.eq]: balcony,
[Op.is]: null [Op.is]: null
} }
}, }
newBuilding: { }
if (newBuilding) {
query.newBuilding = {
[Op.eq]: newBuilding
}
queryIncludeIncomplete.newBuilding = {
[Op.or]: { [Op.or]: {
[Op.eq]: newBuilding, [Op.eq]: newBuilding,
[Op.is]: null [Op.is]: null
} }
}, }
elevator: { }
if (elevator) {
query.elevator = {
[Op.eq]: elevator
}
queryIncludeIncomplete.elevator = {
[Op.or]: { [Op.or]: {
[Op.eq]: elevator, [Op.eq]: elevator,
[Op.is]: null [Op.is]: null
} }
} }
}; }
const order = [["updatedAt", "desc"]]; const order = [["updatedAt", "desc"]];

View File

@@ -26,7 +26,6 @@ const findSearchRequestsForRealEstate = async realEstate => {
realEstateType, realEstateType,
locationLat, locationLat,
locationLong, locationLong,
//
accessRoadType, accessRoadType,
balcony, balcony,
newBuilding, newBuilding,
@@ -51,19 +50,15 @@ const findSearchRequestsForRealEstate = async realEstate => {
const geoSearchQueryPart = sequelize.where(contains, true); const geoSearchQueryPart = sequelize.where(contains, true);
//WIP This wont work, need to separate queries by adType and realEstateType //General query contains only attributes that are defined for every realestate - not null
//because for example flat does not have gardenSize and that can't be in query
const query = { const query = {
adType, adType,
realEstateType, realEstateType,
subscribed: true, subscribed: true,
//
balcony,
newBuilding,
elevator,
[Op.and]: geoSearchQueryPart [Op.and]: geoSearchQueryPart
}; };
//Every other attribute is checked separately and included in query only if it is defined
if (price) { if (price) {
query.priceMin = { query.priceMin = {
[Op.lte]: price [Op.lte]: price
@@ -81,7 +76,7 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.gte]: area [Op.gte]: area
}; };
} }
//
if (gardenSize) { if (gardenSize) {
query.gardenSizeMin = { query.gardenSizeMin = {
[Op.lte]: gardenSize [Op.lte]: gardenSize
@@ -90,6 +85,7 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.gte]: gardenSize [Op.gte]: gardenSize
}; };
} }
if (numberOfRooms) { if (numberOfRooms) {
query.numberOfRoomsMin = { query.numberOfRoomsMin = {
[Op.lte]: numberOfRooms [Op.lte]: numberOfRooms
@@ -98,6 +94,7 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.gte]: numberOfRooms [Op.gte]: numberOfRooms
}; };
} }
if (numberOfFloors) { if (numberOfFloors) {
query.numberOfFloorsMin = { query.numberOfFloorsMin = {
[Op.lte]: numberOfFloors [Op.lte]: numberOfFloors
@@ -106,6 +103,7 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.gte]: numberOfFloors [Op.gte]: numberOfFloors
}; };
} }
if (floor) { if (floor) {
query.floorMin = { query.floorMin = {
[Op.lte]: floor [Op.lte]: floor
@@ -114,6 +112,7 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.gte]: floor [Op.gte]: floor
}; };
} }
if (accessRoadType) { if (accessRoadType) {
query.accessRoadType = { query.accessRoadType = {
[Op.or]: { [Op.or]: {
@@ -122,6 +121,25 @@ const findSearchRequestsForRealEstate = async realEstate => {
} }
} }
} }
if (balcony) {
query.balcony = {
[Op.eq]: balcony
}
}
if (newBuilding) {
query.newBuilding = {
[Op.eq]: newBuilding
}
}
if (elevator) {
query.elevator = {
[Op.eq]: elevator
}
}
return await db.SearchRequest.findAll({ where: query }); return await db.SearchRequest.findAll({ where: query });
}; };