<commit_before>'use strict';

import * as prep from './response-preparation.js';
import {isArray} from 'lodash';

const ResultListTransform = {

  event() {
    return 'getOpenSearchResultList';
  },

  getSearchResultList(request) {
    return this.callServiceClient('opensearch', 'getSearchResult', request);
  },

  /**
   * Transforms the request from the application to Open Search request parameters
   *
   * @param {string} event
   * @param {Object} request
   * @return {Object} request parameters using Open Search terminology
   */

  requestTransform(event, request) {
    let sort = request.sort;

    if (request.sort === 'default') {
      sort = 'rank_frequency';
    }

    return this.getSearchResultList({
      query: request.query,
      start: request.offset,
      stepValue: request.worksPerPage,
      sort: sort,
      facets: {
        facetName: 'term.workType',
        numberOfTerms: 15
      }
    });
  },

  /**
   * Extract facets from the response.
   *
   * @param {Object} response The response from which the facets should be extracted.
   * @return {Array} result Array of facets. Empty array if none is found.
   */
  getFacets(response) {
    const facets = response.result.facetResult.facet || {};
    let result = [];

    if (facets.hasOwnProperty('facetTerm')) {
      const facetTerms = isArray(facets.facetTerm) ? facets.facetTerm : [facets.facetTerm];
      facetTerms.forEach((value) => {
        result.push({
          type: facets.facetName,
          value: value.term,
          displayValue: value.term,
          cssClass: 'worktype'
        });
      });
    }
    return result;
  },

  /**
   * Extract facets from the response.
   *
   * @param {Object} response The response from which the facets should be extracted.
   * @return {Array} result Array of facets. Empty array if none is found.
   */
  prefetchWork(pid) {
    pid = 'rec.id=' + pid;
    const request = {
      query: pid,
      start: 1,
      stepValue: 1,
      allObjects: true
    };
    this.callServiceClient('opensearch', 'getWorkResult', request);
  },

  extractWorkInformation(work) {
    let newWork = {};
    let no = work.collection.numberOfObjects;
    let identifiers = [];
    let title, creator, workType;

    if (no === '1') {
      identifiers.push(work.collection.object.primaryObjectIdentifier || work.collection.object.identifier);
      title = work.formattedCollection.briefDisplay.manifestation.titleFull;
      creator = work.formattedCollection.briefDisplay.manifestation.creator;
      workType = work.formattedCollection.briefDisplay.manifestation.workType;
    }
    else {
      work.collection.object.forEach((identifier) => {
        if (identifier.primaryObjectIdentifier.length || identifier.identifier.length) {
          identifiers.push(identifier.primaryObjectIdentifier || identifier.identifier);
          title = work.formattedCollection.briefDisplay.manifestation[0].titleFull;
          creator = work.formattedCollection.briefDisplay.manifestation[0].creator;
          workType = work.formattedCollection.briefDisplay.manifestation[0].workType;
        }
      });<commit_msg>bug-1234: fix search<commit_after>'use strict';

import * as prep from './response-preparation.js';
import {isArray} from 'lodash';

const ResultListTransform = {

  event() {
    return 'getOpenSearchResultList';
  },

  getSearchResultList(request) {
    return this.callServiceClient('opensearch', 'getSearchResult', request);
  },

  /**
   * Transforms the request from the application to Open Search request parameters
   *
   * @param {string} event
   * @param {Object} request
   * @return {Object} request parameters using Open Search terminology
   */

  requestTransform(event, request) {
    let sort = request.sort;

    if (request.sort === 'default') {
      sort = 'rank_frequency';
    }

    return this.getSearchResultList({
      query: request.query,
      start: request.offset,
      stepValue: request.worksPerPage,
      sort: sort,
      facets: {
        facetName: 'term.workType',
        numberOfTerms: 15
      }
    });
  },

  /**
   * Extract facets from the response.
   *
   * @param {Object} response The response from which the facets should be extracted.
   * @return {Array} result Array of facets. Empty array if none is found.
   */
  getFacets(response) {
    const facets = response.result.facetResult.facet || {};
    let result = [];

    if (facets.hasOwnProperty('facetTerm')) {
      const facetTerms = isArray(facets.facetTerm) ? facets.facetTerm : [facets.facetTerm];
      facetTerms.forEach((value) => {
        result.push({
          type: facets.facetName,
          value: value.term,
          displayValue: value.term,
          cssClass: 'worktype'
        });
      });
    }
    return result;
  },

  /**
   * Extract facets from the response.
   *
   * @param {Object} response The response from which the facets should be extracted.
   * @return {Array} result Array of facets. Empty array if none is found.
   */
  prefetchWork(pid) {
    pid = 'rec.id=' + pid;
    const request = {
      query: pid,
      start: 1,
      stepValue: 1,
      allObjects: true
    };
    this.callServiceClient('opensearch', 'getWorkResult', request);
  },

  extractWorkInformation(work) {
    let newWork = {};
    let no = work.collection.numberOfObjects;
    let identifiers = [];
    let title, creator, workType;

    if (no === '1') {
      identifiers.push(work.collection.object.primaryObjectIdentifier || work.collection.object.identifier);
      title = work.formattedCollection.briefDisplay.manifestation.titleFull;
      creator = work.formattedCollection.briefDisplay.manifestation.creator;
      workType = work.formattedCollection.briefDisplay.manifestation.workType;
    }
    else {
      work.collection.object.forEach((identifier) => {
        if ((identifier.primaryObjectIdentifier && identifier.primaryObjectIdentifier.length) || identifier.identifier.length) {
          identifiers.push(identifier.primaryObjectIdentifier || identifier.identifier);
          title = work.formattedCollection.briefDisplay.manifestation[0].titleFull;
          creator = work.formattedCollection.briefDisplay.manifestation[0].creator;
          workType = work.formattedCollection.briefDisplay.manifestation[0].workType;
        }
      });
{'fraction_non_alphanumeric': 0.11513624192786266, 'fraction_numerical': 0.003150102378327296, 'mean_word_length': 2.946550652579242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567656', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1778, 'n_words': 417}