Tips for managing object references in elasticsearch mongo river and how to resolve them effectively

Can I flatten or resolve references from other collections before indexing into Elasticsearch?

For instance, consider this schema:

var PartSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  province : {
    type : mongoose.Schema.ObjectId,
    ref : 'Province',
    required: true
  },
});

When using mongoriver, the "province" property gets indexed as an ObjectId. This results in the search results displaying the province as an object id, which is not user-friendly. To make it more accessible by users, I need to flat/resolve the province property so that I can access its properties like part.province.name, part.province.createdAt, etc.

My initial approach involved script filters and mappings. Here's what I did:

First, I defined a mapping on ES:

curl -XPUT 'http://localhost:9200/parts/part/_mapping' -d '{"properties":{"__v":{"type":"long"},
"title":{"type":"string"},
"province":{
    "type":"nested",
    "properties": {
        name : {"type": "string"}
    }
}}}'

Then, I created a river:

curl -XPUT "localhost:9200/_river/pdm/_meta" -d '
{
  "type": "mongodb",
  "mongodb": {
    "servers": [
      { "host": "localhost", "port": 27017 }
    ],
    "db": "pdm",
    "collection": "parts"
  },
  "index": {
    "name": "parts",
    "type": "part"
  }
}'

Lastly, I developed a script:

ctx.document.province = {};
ctx.document.province.name = 'Static name to be inserted by script';

This solution works, but currently, the name in the script is static. I need to dynamically fetch it from the MongoDB database. My attempt to do this via a REST API using AJAX with lang-javascript failed due to limitations outside the browser. Even if successful, I'm unsure of its efficiency.

Do you have any suggestions on how to address this issue? Or are there alternative methods to flatten/reference object references before indexing into ES using mongoriver? Any insights would be appreciated.

Thanks in advance :)

Note: I also need to automatically update object references in previously indexed documents when modified.

Related issue: https://groups.google.com/forum/#!topic/elasticsearch/e3CelbOkgWk

Answer №1

In my opinion, the most straightforward approach to accomplish this task would involve utilizing a cron job or a synchronization application. Why do I suggest this method?

  1. With Elasticsearch planning to deprecate rivers
  2. You can have full control over the availability of related data
  3. You will be able to implement complex queries to determine what is indexed (for example, if a user's information is updated, all of their stored data can be updated as well)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using JavaScript to modify the -webkit-animation-play-state

Hello, I'm currently facing a challenge in changing my css3 -webkit-animation-play-state property from paused to running upon clicking on another div. Does anyone have any suggestions on how I can achieve this? I believe JavaScript might be the way to ...

ReactJS issue: I am unable to display the dropdown menu while looping through an array

Although the title may be confusing, I am facing an issue in my Laravel project that integrates ReactJS on the front end. I am attempting to create a dropdown menu for users to select the status of a project. The statuses and their IDs are retrieved from a ...

What is the best way to choose a specific row with Enzyme?

We have chosen Jest for doing UI Test-Driven Development on our React application. Our component rendering structure looks like this: <Div> <Row> </Row> <ROW> <Row> <ROW> <Link> <Link> ...

Utilizing Angular's ng-repeat with varying directives for each iteration

I am trying to utilize ng-repeat within Angular to iterate through multiple <li> elements with a directive embedded. My goal is to have the first two items in the ng-repeat display differently in terms of styling and content compared to the remaining ...

What steps can be taken to resolve webpage loading issues following data insertion into a database using express and node?

My express app has a post route handler (/addpost) for adding data to a database. Everything works perfectly, but the window keeps loading. To prevent the browser from waiting for more data, I know I need to send a response. However, all I want is for th ...

Sequencing asynchronous functions in Angular: Ensuring one function runs before another

When working with a save function that requires you to call another function to retrieve the revision number and make an API call, both of which are asynchronous in nature, how can you ensure one function waits for the other to execute? $scope.getRevision ...

Unspecified variable in AngularJS data binding with Onsen UI

I am new to Onsen UI and AngularJS, and I have a simple question about data binding. When I use the variable $scope.name with ng-model in an Onsen UI template, it returns as UNDEFINED. Here is my code: <!doctype html> <html lang="en" ng-app="simp ...

Disappearing Into the Background Excluding Specific Divs

I have a dilemma with fading in and out a background image using jQuery fadeIn and fadeOut. The issue arises because my wrapper div contains elements such as sidebar and navigation that are positioned absolutely within the wrapper div, causing them to also ...

Complete picture in a circular div with aspect ratio

I'm currently working on creating a profile page and I'd like to have an image inside a circular div. The challenge is that I want the image to maintain its aspect ratio, even though the dimensions are unknown and users can upload images of any s ...

Efficiently centering content in a grid layout using automatic fit repetition for optimized responsiveness

I've implemented a responsive grid where each item has its own hidden details section that is revealed upon clicking the item. The structure of the HTML/CSS setup is as follows: <div class="grid"> <div class="item"> ...

Creating a left join query builder in Symfony for MongoDB ODM

I have a scenario where I have two ODM documents. The first one is an Item document with the following structure: class Items { /** * @MongoDB\Field(name="item_name", type="string") */ protected $itemName; } The second document is ItemLocation, ...

Guide on converting data from PHP to JSON using JavaScript

Here is some data that I need to loop through: image1 url1 description1 image2 url2 description2 image3 url3 description3 image4 url4 description4 image5 url5 description5 After looping through the data: var imagesDataArray = [ { src: &apo ...

Tips for incorporating several d3 elements on a single webpage

I am currently facing an issue where I am attempting to add a line graph below a d3 map, but the map and line graph are appearing below where the map should be located. I have tried creating separate div tags with distinct id's, but I am unsure of wha ...

Error 400 encountered when making a POST request on the DreamFactory API

Attempting to send a request from my Python application to a MongoDB setup on my localhost using dreamfactory API. However, I keep receiving a 400 response. Connectivity to MongoDB is confirmed as the post request works without issue from the Dreamfactor ...

Leveraging JavaScript to extract data from a JSON file upon clicking a button

Currently, I am working on a problem where the user enters values into a search box, clicks the search button, and then with the onClick event, the search terms are compared to values in a JSON file. I do not have knowledge of jQuery, so if there is a solu ...

Choose a specific <div> element from an external page using Ajax/PHP

I'm encountering a small issue. I am currently utilizing Ajax to dynamically load content from another page into a <div> element after detecting a change in a <select>. However, my specific requirement is to only load a particular <div& ...

Creating a dynamic Bootstrap carousel with a fixed hero header text inside a designated container - here's how!

I am currently working on a project to develop a responsive Bootstrap slider with a fixed hero header that remains consistent while the slider images change. The hero header needs to be aligned to the left within a responsive Bootstrap container and center ...

Exploring the Ways to Share sessionStorage Across Multiple Browser Tabs Using JavaScript

I have recently started exploring client-side data storage and have successfully implemented an onkeyup search function. The goal is to retrieve the city name via AJAX and display it at the top within the header of the page. To store the city name, I have ...

Display array elements without numerical indexes

Consider the code snippet below: for(i=0; i<3; i++){ a = {}; a['name' + i] = i; data.push(a); } This code will generate the following array: { 1:{name0:0}, 2:{name1:1}, 3:{name2:2} } How can I modify the code ...

JavaScript in fullscreen mode for Internet Explorer

I'm trying to make sure that this code snippet... $('#gatewayDimmer').width($('html').width()); $('#gatewayDimmer').height($('html').height()); $('#gatewayDimmer').css('display','block& ...