Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation of returned _source data in results by highlightResults function #10

Open
mahaffey opened this issue May 25, 2018 · 2 comments

Comments

@mahaffey
Copy link
Contributor

When a user specifies they want their data to be highlighted the highlighted data returns a truncated portion in the _source.

Please see my comments below. I didn't think a pull request was appropriate as all I would be doing is deleting a function.

Is there a reason for mutating the source of the highlighted data?

Best,
Ryan

// src/utils/helper.js


// Why are we mutating the original _source object here?  
// This takes away information for the end-user given by elasticsearch

// My use case of ElasticSearch is to store parsed pdfs as strings 
// These contain 1000+ chars each

// The highlighted information given by elasticsearch have many fewer characters than this. 
// Around 100 chars 

// I want to be able to send the whole of the information to the end-user in the results

// If you remove this function (highlightResults)
// the user still retains the highlighted information in the main result object
//  and the original _source information

const highlightResults = (result) => {
	const data = { ...result };
	if (data.highlight) {
		Object.keys(data.highlight).forEach((highlightItem) => {
			const highlightValue = data.highlight[highlightItem][0];
			data._source = Object.assign({}, data._source, { [highlightItem]: highlightValue });
		});
	}
	return data;
};

export const parseHits = (hits) => {
	let results = null;
	if (hits) {
		results = [...hits].map((item) => {
			const streamProps = {};

			if (item._updated) {
				streamProps._updated = item._updated;
			} else if (item._deleted) {
				streamProps._deleted = item._deleted;
			}



                        // no need to mutate the data! below

			const data = highlightResults(item);

			return {
				_id: data._id,
				_index: data._index,
				...data._source,
				...streamProps,
			};
		});
	}
	return results;
};
@metagrover
Copy link
Contributor

I agree, you have a point here. We only mutate the hits data in order to render the highlighted fields in the result-component directly. This lets users render highlighted results easily without worrying about processing the highlighted fields.

But I believe, we should come up with an approach where either both of these behaviors are feasible by default or it is toggle-able.

@mahaffey
Copy link
Contributor Author

Left a pull request. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants