diff --git a/src/components/vs-input/vs-input.ts b/src/components/vs-input/vs-input.ts index 00f935b..8bb0690 100644 --- a/src/components/vs-input/vs-input.ts +++ b/src/components/vs-input/vs-input.ts @@ -88,7 +88,12 @@ export default class VsInput extends Vue { // }; const suggestions = new Array(); + console.log("Display:", this.display); + this.results.forEach((dataset) => { + + console.log("suggestions:foreach:", dataset.id); + // const del = dataset.title_output?.toLowerCase(); if (dataset.title_output.toLowerCase().includes(this.display.toLowerCase())) { const title = dataset.title_output; @@ -168,7 +173,7 @@ export default class VsInput extends Vue { } private request(): void { - console.log("searchTerm"); + console.log("request()"); DatasetService.searchTerm(this.display, this.solr.core, this.solr.host).subscribe({ next: (res: Dataset[]) => this.dataHandler(res), error: (error: string) => this.errorHandler(error), @@ -178,6 +183,8 @@ export default class VsInput extends Vue { private dataHandler(datasets: Dataset[]): void { this.results = datasets; + // console.log(datasets); + // this.$emit("search", this.display); // this.loading = false; } diff --git a/src/services/dataset.service.ts b/src/services/dataset.service.ts index 95208dc..e510772 100644 --- a/src/services/dataset.service.ts +++ b/src/services/dataset.service.ts @@ -9,34 +9,228 @@ import { VUE_API } from "@/constants"; import { deserialize } from "class-transformer"; class DatasetService { - /* Initial test method to fetch and log data from the local OpenSearch endpoint (new backend) */ + // /* Initial test method to fetch and log data from the local OpenSearch endpoint (new backend) */ + // async fetchDataFromOpenSearch(searchTerm: string): Promise { + // const url = "http://192.168.21.18/tethys-records/_search"; + // const headers = { + // "Content-Type": "application/json", + // }; + // const body = { + // query: { + // match: { + // title: searchTerm, + // }, + // }, + // }; + + // try { + // const response = await fetch(url, { + // method: "POST", + // headers: headers, + // body: JSON.stringify(body), + // }); + + // if (!response.ok) { + // throw new Error(`Failed to fetch data from ${url}, status: ${response.status}`); + // } + + // const data = await response.json(); + // console.log("Data from OpenSearch:", data); + // } catch (error) { + // console.error("Error fetching data:", error); + // } + // } + + /** + * Fetch data from the OpenSearch endpoint with fuzzy search enabled. + * This function allows for misspellings in the search term and boosts + * the relevance of matches in the title, author, and subject fields. + * + * @param {string} searchTerm - The search term to query. + */ async fetchDataFromOpenSearch(searchTerm: string): Promise { + // Define the OpenSearch endpoint URL const url = "http://192.168.21.18/tethys-records/_search"; + + // Set the headers for the POST request const headers = { "Content-Type": "application/json", }; + + // Construct the body of the POST request const body = { query: { - match: { - title: searchTerm, - }, + bool: { + // The `should` clause specifies that at least one of these conditions must match + should: [ + { + // Match the search term in the title field with fuzziness enabled and a boost of 3 + match: { + title: { + query: searchTerm, + fuzziness: "AUTO", // Enable fuzzy search + boost: 3 // Boosting the relevance of title matches + } + } + }, + { + // Match the search term in the author field with fuzziness enabled and a boost of 2 + match: { + author: { + query: searchTerm, + fuzziness: "AUTO", // Enable fuzzy search + boost: 2 // Boosting the relevance of author matches + } + } + }, + { + // Match the search term in the subject field with fuzziness enabled and a boost of 1 + match: { + subject: { + query: searchTerm, + fuzziness: "AUTO", // Enable fuzzy search + boost: 1 // Boosting the relevance of subject matches + } + } + }, + { + // Match the search term in the title field with a wildcard + wildcard: { + title: { + value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + boost: 3 // Boosting the relevance of title matches + } + } + }, + { + // Match the search term in the author field with a wildcard + wildcard: { + author: { + value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + boost: 2 // Boosting the relevance of author matches + } + } + }, + { + // Match the search term in the subject field with a wildcard + wildcard: { + subject: { + value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + boost: 1 // Boosting the relevance of subject matches + } + } + } + ], + // Ensure that at least one of the `should` clauses must match + minimum_should_match: 1 + } }, + // Limit the number of search results to 10 + size: 10, + // Start from the first result (pagination) + from: 0, + // Sort the results by the `server_date_published` field in descending order + sort: [ + { server_date_published: { order: "desc" } } + ], + // Aggregations to provide facets for the `language` and `subject` fields + aggs: { + language: { + terms: { + field: "language.keyword" // Aggregate by the exact values of the `language` field + } + }, + subject: { + terms: { + field: "subjects.keyword", // Aggregate by the exact values of the `subjects` field + size: 10 // Limit the number of aggregation buckets to 10 + } + } + } }; + // // Construct the body of the POST request + // const body = { + // query: { + // bool: { + // // The `should` clause specifies that at least one of these conditions must match + // should: [ + // { + // // Match the search term in the title field with a wildcard + // wildcard: { + // title: { + // value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + // boost: 3 // Boosting the relevance of title matches + // } + // } + // }, + // { + // // Match the search term in the author field with a wildcard + // wildcard: { + // author: { + // value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + // boost: 2 // Boosting the relevance of author matches + // } + // } + // }, + // { + // // Match the search term in the subject field with a wildcard + // wildcard: { + // subject: { + // value: `${searchTerm}*`, // Wildcard search for terms starting with searchTerm + // boost: 1 // Boosting the relevance of subject matches + // } + // } + // } + // ], + // // Ensure that at least one of the `should` clauses must match + // minimum_should_match: 1 + // } + // }, + // // Limit the number of search results to 10 + // size: 10, + // // Start from the first result (pagination) + // from: 0, + // // Sort the results by the `server_date_published` field in descending order + // sort: [ + // { server_date_published: { order: "desc" } } + // ], + // // Aggregations to provide facets for the `language` and `subject` fields + // aggs: { + // language: { + // terms: { + // field: "language.keyword" // Aggregate by the exact values of the `language` field + // } + // }, + // subject: { + // terms: { + // field: "subjects.keyword", // Aggregate by the exact values of the `subjects` field + // size: 10 // Limit the number of aggregation buckets to 10 + // } + // } + // } + // }; + try { + // Send the POST request to the OpenSearch endpoint const response = await fetch(url, { method: "POST", headers: headers, body: JSON.stringify(body), }); + // Check if the response is not successful if (!response.ok) { throw new Error(`Failed to fetch data from ${url}, status: ${response.status}`); } + // Parse the response JSON const data = await response.json(); + // Log the data from OpenSearch console.log("Data from OpenSearch:", data); + console.log("Hits:", data.hits.total.value); } catch (error) { + // Log any errors that occur during the fetch process console.error("Error fetching data:", error); } }