Creating a new table in mongoose based on object IDs - javascript

Hello the title of this question is very poorly worded however i will better explain the issue.
I have a document called 'buildings', and I also have two documents called 'rooms' and 'logins'. In both rooms and logins, they have an embedded document of building as follows:
Room Schema:
const roomSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
building: {
type: new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
}
}),
required: true
}
});
Logins Schema:
const loginSchema = new mongoose.Schema({
user: {
type: new mongoose.Schema({
email: {
type: String,
required: true
},
isVisitor: {
type: Boolean,
required: true
}
})
},
dateIn: {
type: Date,
required: true,
default: Date.now()
},
dateOut: {
type: Date
},
building: {
type: new mongoose.Schema({
name: {
type: String,
required: true
}
})
}
});
And the building schema is as follows:
const Building = mongoose.model(
"Building",
new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
address: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
postcode: {
type: String,
required: true,
minlength: 6,
maxlength: 8
},
organisation: {
type: new mongoose.Schema({
name: {
type: String,
required: true
}
})
}
})
);
Here is some examples of documents from each schema:
Building:
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "TusPark Newcastle"
},
"__v": 0
},
Rooms:
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark"
},
"__v": 0
},
Logins:
{
"dateIn": "2019-03-21T13:13:23.069Z",
"_id": "5c938e151bd7a02d479893bd",
"user": {
"_id": "5c925378e88bb72764283108",
"email": "jack1#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "TusPark"
},
"__v": 0
}
I am trying to send a document to the client which contains the building details, with the rooms and logins embedded as attributes for the relevant building. The code I currently have written for this is below:
router.get("/all", async (req, res) => {
const buildings = await Building.find();
const rooms = await Room.find({
building_id: buildings._id
});
const logins = await Login.find({
building_id: buildings._id
});
const building_rooms = await Building.aggregate([
{
$lookup: {
from: "rooms",
localField: "building_id",
foreignField: "building_id",
as: "building_rooms"
}
},
{
$lookup: {
from: "logins",
localField: "building_id",
foreignField: "building_id",
as: "building_logins"
}
}
]);
res.send(building_rooms);
});
Note I have purposely not included $unwind
The current output for this request is:
[
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "Park Newcastle"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
},
{
"_id": "5c925475e88bb72764283113",
"name": "Manchester",
"site": {
"_id": "5c921c94a922f6236cbe37d2",
"name": "Manchester"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
}
]
However the issue is that the logins and rooms of 'Park' are being embedded in manchester building. which is not correct.
The correct output should be:
[
{
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park",
"site": {
"_id": "5c79a243d2016312ecd27c45",
"name": "Park Newcastle"
},
"__v": 0,
"building_rooms": [
{
"_id": "5c7fa01abdd6233d6fdbc917",
"name": "E101",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
},
{
"_id": "5c7fdbd12229db40589e41b5",
"name": "E202",
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0
}
],
"building_logins": [
{
"_id": "5c91483402d1a4145a2c4d9b",
"dateIn": "2019-03-19T19:51:06.458Z",
"user": {
"_id": "5c83b4b87321805bad025e65",
"email": "bob1999#gmail.com",
"isVisitor": true
},
"building": {
"_id": "5c79a31ed2016312ecd27c46",
"name": "Park"
},
"__v": 0,
"dateOut": "2019-03-20T11:55:19.205Z"
}
]
},
{
"_id": "5c925475e88bb72764283113",
"name": "Manchester",
"site": {
"_id": "5c921c94a922f6236cbe37d2",
"name": "Manchester"
},
"__v": 0,
"building_rooms": [],
"building_logins": []
}
]
With there being no instances of building_rooms or building_logs as there are no logins or rooms with the object ID relating to manchester.
I really appreciate the time taken to read this and would be grateful for any help with solving tis problem.

Related

MongoDB : - Merge object with same key into one

I am trying to merge an object inside an array with the same date but with a different key name for the status key.
I have 2 collections users and canteens
The query I am trying to get the result but am not able to figure out how to merge the object with the same Date
OUTPUT
User.aggregate([
{ $sort: { workerId: 1 } },
{
$lookup: {
from: "canteens",
localField: "_id",
foreignField: "employeeId",
pipeline: [
{
$match: {
Date: {
$gte: new Date(fromDate),
$lte: new Date(toDate),
},
},
},
{
$project: {
Date: 1,
status: 1,
},
},
],
as: "canteens",
},
},
{
$project: {
_id: 1,
workerId: 1,
workerFirstName: 1,
workerSurname: 1,
workerDepartment: 1,
workerDesignation: 1,
locationName: 1,
canteenData: "$canteens",
},
},
]);
[
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstaname",
"workerSurname": "lastname",
"workerId": "1",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status": "LUNCH",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b2db8db10c24487201e0a2",
"status": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status": "BREAK FAST",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b3d248c076184fb07ff2c4",
"status": "LUNCH",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b42b8ccb57a4cb7af34015",
"status": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
},
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstaname1",
"workerSurname": "lastname1",
"workerId": "2",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status": "LUNCH",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b2db8db10c24487201e0a2",
"status": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status": "BREAK FAST",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b3d248c076184fb07ff2c4",
"status": "LUNCH",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b42b8ccb57a4cb7af34015",
"status": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
}
]
The output I am trying to get
[
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstanem",
"workerSurname": "lastname",
"workerId": "1",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status1": "LUNCH",
"status2": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status1": "BREAK FAST",
"status2": "LUNCH",
"status3": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status1": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
},
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstanem1",
"workerSurname": "lastname1",
"workerId": "2",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status1": "LUNCH",
"status2": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status1": "BREAK FAST",
"status2": "LUNCH",
"status3": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status1": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
}
]
One option is to add 2 steps into your $lookup pipeline aggregation:
{$group: {
_id: "$Date",
_idVal: {$first: "$_id"},
data: {$addToSet: "$status"}
}},
{$replaceRoot: {
newRoot: {
$mergeObjects: [
{_id: "$_idVal", Date: "$_id"},
{$arrayToObject: {
$reduce: {
input: "$data",
initialValue: [],
in: {$concatArrays: [
"$$value",
[{k: {$concat: [
"status",
{$toString: {$add: [{$size: "$$value"}, 1]}}
]},
v: "$$this"}]
]}
}
}}
]
}
}}
See how it works on the playground example
It's not easy to create status1, status2, ... variables dynamically + how do we know BREAK FAST should be status1 and not status2.
Alternative solution: We $group inside correlated subqueries and push all status values into an array
db.users.aggregate([
{
"$lookup": {
"from": "canteens",
"localField": "_id",
"foreignField": "employeeId",
pipeline: [
{
// Put your custom filters here
$match: {}
},
{
$group: {
_id: "$Date",
//pick "first" canteens _id
id: {
$first: "$_id"
},
status: {
$push: "$status"
}
}
},
{
$project: {
_id: "$id",
Date: "$_id",
status: 1
}
},
],
as: "canteenData",
}
}
])
MongoPlayground

How to group result of lookup aggregate results

I aggregate users document:
User.aggregate([
{
$match: { _id: req.params.id },
},
{
$lookup: {
from: 'moods',
localField: '_id',
foreignField: 'source.userId',
pipeline: [
{
$lookup: {
from: 'contactrequests',
localField: 'source.userId',
foreignField: 'source.userId',
let: {
// truncate timestamp to start of day
moodsDate: {
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
},
pipeline: [
{
$match: {
$expr: {
$eq: [
'$$moodsDate',
{
// truncate timestamp to start of day
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
],
},
},
},
],
as: 'contactRequests',
},
},
],
as: 'calendar',
},
},
]).exec()
There is final document what I get
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"lastName": "Doe",
"login": "User-01",
"name": "John"
"calendar": [
{
"_id": "62a351e33859aaf975c63323",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-07T12:44:13.333Z",
"mood": "good",
"contactRequests": []
},
{
"_id": "62a351f43859aaf975c63327",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-09T12:44:13.333Z",
"mood": "middle",
"contactRequests": [
{
"timestamp": "2022-06-09T12:44:13.333Z",
"source": {
"deviceId": "Pacjent-141214",
"userId": "P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329",
},
]
}
]
},
This is what I would to get. This is more clean and readable.
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"login": "User-01",
"name": "John",
"lastName": "Doe",
"calendar": [
{
"timestamp": "2022-06-11T12:44:13.333Z"
"mood": {
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
},
"timestamp": "2022-06-11T12:44:13.333Z",
"mood": "bad",
"_id": "62a352b83859aaf975c6332d",
},
"contactRequest": [
{
"timestamp": "2022-06-11T15:25:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": true,
"_id": "62a351ff3859aaf975c63329"
},
{
"timestamp": "2022-06-11T18:23:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329"
},
]
}
}
]
}
To achive that I've used $group parameter, but at some point I have to declare which field should be fetch to result document and I have problem with contatRequest fields.
{
$group: {
_id: {
$dateToString: {
format: '%Y-%m-%d',
date: '$timestamp',
},
},
mood: {
$push: {
_id: '$_id',
source: '$source',
type: '$mood',
timestamp: '$timestamp',
},
},
contactRequest: {
$push: {
_id: '$contactRequest._id',
source: '$contactRequest.source',
resolve: '$contactRequest.resolve',
timestamp: '$contactRequest.timestamp',
},
},
},
},
{
$project: {
_id: 0,
timestamp: '$_id',
mood: 1,
contactRequest: 1,
},
},
Sample database/collections/aggregation pipeline at mongoplayground.net.

Data structure of Threaded comments in mongoose and Node Js

{
"post": {
"img": "",
"likes": [
"60c418582f7066090ced4a51"
],
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [
{
"comments": [],
"_id": "60d6ab9207c0a573786a9e65",
"userId": "60c418582f7066090ced4a51",
"content": "good post 3",
"createdAt": "2021-06-26T04:22:42.337Z",
"updatedAt": "2021-06-26T04:22:42.337Z",
"__v": 0
}
],
"_id": "60d6962cee10aa73f820b974",
"userId": "60c418582f7066090ced4a51",
"content": "good post 2",
"createdAt": "2021-06-26T02:51:24.111Z",
"updatedAt": "2021-06-26T04:22:42.622Z",
"__v": 0
},
{
"comments": [],
"_id": "60d705784ab01c354cf7f445",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "Comment deleted by User",
"createdAt": "2021-06-26T10:46:16.813Z",
"updatedAt": "2021-06-26T12:29:06.398Z",
"__v": 0
},
{
"comments": [],
"_id": "60d706febcba957b04406547",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "yes it is a good post 1 from alexane Updated",
"createdAt": "2021-06-26T10:52:46.679Z",
"updatedAt": "2021-06-26T12:17:58.879Z",
"__v": 0
}
],
"_id": "60d695b4ee10aa73f820b973",
"userId": "60c418582f7066090ced4a51",
"content": "good post 1",
"createdAt": "2021-06-26T02:49:24.426Z",
"updatedAt": "2021-06-26T12:30:44.872Z",
"__v": 0
}
],
"_id": "60d68e32dff84439a4d3b191",
"userId": "60c418582f7066090ced4a51",
"content": "good post",
"createdAt": "2021-06-26T02:17:22.625Z",
"updatedAt": "2021-06-26T02:49:24.820Z",
"__v": 0
},
{
"comments": [
{
"comments": [],
"_id": "60d6c2d917d0b12be44742d2",
"userId": "60c418582f7066090ced4a51",
"content": "nice post 1",
"createdAt": "2021-06-26T06:02:01.420Z",
"updatedAt": "2021-06-26T06:02:01.420Z",
"__v": 0
}
],
"_id": "60d6bebf17d0b12be44742d1",
"userId": "60c418582f7066090ced4a51",
"content": "nice post",
"createdAt": "2021-06-26T05:44:31.436Z",
"updatedAt": "2021-06-26T06:02:01.676Z",
"__v": 0
},
{
"comments": [
{
"comments": [],
"_id": "60d87e7df43fed7e4079875e",
"userId": "60c15ac41ed8da1ab4efe7f3",
"content": "awesome post 1",
"createdAt": "2021-06-27T13:34:53.192Z",
"updatedAt": "2021-06-27T13:34:53.192Z",
"__v": 0
}
],
"_id": "60d87cb4f43fed7e4079875d",
"userId": "60c418582f7066090ced4a51",
"content": "awesome post",
"createdAt": "2021-06-27T13:27:16.299Z",
"updatedAt": "2021-06-27T13:34:53.468Z",
"__v": 0
}
],
"_id": "60c5a23eb37b425a00968fa9",
"userId": "60c418582f7066090ced4a51",
"description": "This is a sample Post 2",
"createdAt": "2021-06-13T06:14:22.196Z",
"updatedAt": "2021-06-27T13:27:16.577Z",
"__v": 0
}
}
I have a threaded comments for a single post which I recursively autopopulated using Mongoose (the code can be found below ). Is it correct to assume the above data structure is a Tree ? If yes, what kind of tree data structure would be the best to implement a a threaded comments to do basic crud such as insert , edit and delete.
//Comment.js
const mongoose = require('mongoose')
const { Schema } = mongoose
const commentObj = {
userId: {
type: mongoose.Types.ObjectId,
ref: "User",
required: true
},
content: {
type: String,
min: 6,
required: true
},
comments: [
{ type: Schema.Types.ObjectId, ref: 'Comment' }
],
}
const commentSchema = new Schema(commentObj, { timestamps: true });
const autoPopulateChildren = function (next) {
this.populate('comments');
next();
};
commentSchema
.pre('findOne', autoPopulateChildren)
.pre('find', autoPopulateChildren)
const Comment = mongoose.model('Comment', commentSchema);
module.exports = Comment
//Post.js
const mongoose = require('mongoose')
const { Schema } = mongoose
const postObj = {
userId: {
type: mongoose.Types.ObjectId,
ref: "User",
required: true
},
description: {
type: String,
min: 6,
required: true
},
img: {
type: String,
default: '',
},
likes: [
{
type: mongoose.Types.ObjectId,
ref: "User"
}
],
comments: [
{
type: mongoose.Types.ObjectId,
ref: "Comment",
}
]
}
const postSchema = new Schema(postObj, { timestamps: true });
const Post = mongoose.model('Post', postSchema);
module.exports = Post
Thank you.

unable to populate articles in mongoose [duplicate]

This question already has answers here:
How to convert string to objectId in LocalField for $lookup Mongodb [duplicate]
(1 answer)
Querying after populate in Mongoose
(6 answers)
Closed 4 years ago.
I have the category schema like this:
var category_article_Schema = new Schema({
"article_id": { type: String, required: false, unique: false },
"category": String,
"articles": [{ type: mongoose.Schema.Types.ObjectId, ref:'article'}],
});
var category_article_Schema = mongoose.model('category_article_Schema', category_article_Schema);
module.exports = category_article_Schema;
Article schema:
var articleSchema = new Schema({
"title": { type: String, required: true, unique: false },
"details": String,
"username": { type: String, required: true, unique: false },
"postImageUrl": String,
"url": String,
"categories": [String],
"created_at": { type: Date, default: Date.now }
});
var article = mongoose.model('article', articleSchema);
module.exports = article;
When I try to populate articles in the below function, I get only category id and name but no articles populated.
function getPostByCategory(req, res, next) {
category_article_model.find({category: req.params.name}) //sports
.populate('articles')
.exec()
.then(function(articlesByCategory) {
console.log(articlesByCategory);
})
.catch(function(err){
console.log('err ', err);
})
}
All the data I have in article collection is this:
`{
"_id": {
"$oid": "5b0008ce8787890004989df1"
},
"categories": [
"sports",
"health"
],
"title": "sample",
"details": "sample ",
"created_at": {
"$date": "2018-05-19T11:21:50.837Z"
},
"__v": 0
},
{
"_id": {
"$oid": "5b0087646dda9600049a9a27"
},
"categories": [
"sports"
],
"title": "sample3333",
"details": " sample3333",
"created_at": {
"$date": "2018-05-19T20:21:56.126Z"
},
"__v": 0
}`
And category_article_schema collection has:
{
"_id": {
"$oid": "5b0087646dda9600049a9a28"
},
"category": "sports",
"article_id": "5b0087646dda9600049a9a27",
"__v": 0
}
But the data returned is empty array of article:
[ { articles: [],
_id: 5b0008ce8787890004989df2,
category: 'sports',
article_id: '5b0008ce8787890004989df1',
__v: 0 } ]
I am not sure what could be the issue?

How to build the proper mapping/indexing in ElasticSearch with NodeJS

I have been beating my head against this all day, and cannot seem figure out how to get this to work.
I have a source document like this:
{
"created_at": 1454700182,
"message_id": 160,
"user_id": 1,
"establishment_id": 1,
"geo": {
"coordinates": [-4.8767633,
89.7833547
],
"type": "Point"
},
"message": "Venus is in the west",
"active": true,
"score": 0,
"name": {
"first": "First",
"last": "Last"
},
"neighborhood": "Townside"
},
I create a document like this in ElasticSearch:
{
"message_id": 160,
"message": "Venus is in the west",
"first_name": "First",
"last_name": "Last",
"location": {
"lon": -4.8767633,
"lat": 89.7833547
},
"created_at": 1454700182,
"neighborhood": "Townside"
}
I've been trying different ways to create the index.
First:
client.indices.create({
index: 'messages',
type: 'document',
body: {
messages: {
properties: {
message: {
type: 'string',
index: 'not_analyzed'
},
neighborhood: {
type: 'string',
index: 'not_analyzed'
},
first_name: {
type: 'string',
index: 'not_analyzed'
},
last_name: {
type: 'string',
index: 'not_analyzed'
},
created_at: {
type: 'integer',
index: 'not_analyzed'
},
location: {
type: 'geo_point',
lat_lon: true
}
}
}
},
}
);
This allows me to do fuzzy text searches and greater than queries, but doesn't recognize the geo_point. So I tried this:
client.indices.create({
index: 'messages',
type: 'document',
"mappings": {
"messages": {
"properties": {
"message": {
"type": "string",
"index": "not_analyzed"
},
"neighborhood": {
"type": "string",
"index": "not_analyzed"
},
"first_name": {
"type": "string",
"index": "not_analyzed"
},
"last_name": {
"type": "string",
"index": "not_analyzed"
},
"created_at": {
"type": "integer",
"index": "not_analyzed"
},
"location": {
"type": "geo_point",
"lat_lon": true,
"index": "not_analyzed"
}
}
}
}
});
This does recognize the geo_point, but none of the other things work.
Here is the query I've been using for the non geo fields:
query = {
query: {
filtered: {
query: {
multi_match: {
query: message,
fields: ['message', 'neighborhood', 'first_name', 'last_name'],
"fuzziness": "AUTO",
"prefix_length": 2
}
},
filter: {
bool: {
must: {
range: {
"created_at": {
"gte": min_ts
}
}
}
}
}
}
}
};
I've been so turned around on this, just trying to allow text and geo search on the same collection of documents, that I need at least another set of eyes.
Appreciate any help!

Categories

Resources