Progress with OpenSearch tests with fuzzy search and wildcard

This commit is contained in:
Porras-Bernardez 2024-05-28 16:33:55 +02:00
parent 484be48d1e
commit a853e93b68
2 changed files with 206 additions and 5 deletions

View File

@ -88,7 +88,12 @@ export default class VsInput extends Vue {
// }; // };
const suggestions = new Array<Suggestion>(); const suggestions = new Array<Suggestion>();
console.log("Display:", this.display);
this.results.forEach((dataset) => { this.results.forEach((dataset) => {
console.log("suggestions:foreach:", dataset.id);
// const del = dataset.title_output?.toLowerCase(); // const del = dataset.title_output?.toLowerCase();
if (dataset.title_output.toLowerCase().includes(this.display.toLowerCase())) { if (dataset.title_output.toLowerCase().includes(this.display.toLowerCase())) {
const title = dataset.title_output; const title = dataset.title_output;
@ -168,7 +173,7 @@ export default class VsInput extends Vue {
} }
private request(): void { private request(): void {
console.log("searchTerm"); console.log("request()");
DatasetService.searchTerm(this.display, this.solr.core, this.solr.host).subscribe({ DatasetService.searchTerm(this.display, this.solr.core, this.solr.host).subscribe({
next: (res: Dataset[]) => this.dataHandler(res), next: (res: Dataset[]) => this.dataHandler(res),
error: (error: string) => this.errorHandler(error), error: (error: string) => this.errorHandler(error),
@ -178,6 +183,8 @@ export default class VsInput extends Vue {
private dataHandler(datasets: Dataset[]): void { private dataHandler(datasets: Dataset[]): void {
this.results = datasets; this.results = datasets;
// console.log(datasets);
// this.$emit("search", this.display); // this.$emit("search", this.display);
// this.loading = false; // this.loading = false;
} }

View File

@ -9,34 +9,228 @@ import { VUE_API } from "@/constants";
import { deserialize } from "class-transformer"; import { deserialize } from "class-transformer";
class DatasetService { 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<void> {
// 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<void> { async fetchDataFromOpenSearch(searchTerm: string): Promise<void> {
// Define the OpenSearch endpoint URL
const url = "http://192.168.21.18/tethys-records/_search"; const url = "http://192.168.21.18/tethys-records/_search";
// Set the headers for the POST request
const headers = { const headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
}; };
// Construct the body of the POST request
const body = { const body = {
query: { query: {
match: { bool: {
title: searchTerm, // 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 { try {
// Send the POST request to the OpenSearch endpoint
const response = await fetch(url, { const response = await fetch(url, {
method: "POST", method: "POST",
headers: headers, headers: headers,
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
// Check if the response is not successful
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch data from ${url}, status: ${response.status}`); throw new Error(`Failed to fetch data from ${url}, status: ${response.status}`);
} }
// Parse the response JSON
const data = await response.json(); const data = await response.json();
// Log the data from OpenSearch
console.log("Data from OpenSearch:", data); console.log("Data from OpenSearch:", data);
console.log("Hits:", data.hits.total.value);
} catch (error) { } catch (error) {
// Log any errors that occur during the fetch process
console.error("Error fetching data:", error); console.error("Error fetching data:", error);
} }
} }