function semantic_search() {
if ($("#search_query").val() != "") {
$("#tag_list").empty();
$("#wordcloud").empty();
$("#results").empty();
$("#add_keywords").empty();
$("#add_labels").empty();
$("#add_names").empty();
$("#wordcloud").append(`
Loading...
`);
top_k = parseInt($("#top_k").val());
if (isNaN(top_k)) {
top_k = 100;
}
similarity = parseFloat($("#similarity").val());
if (isNaN(similarity)) {
similarity = 0.0;
}
data = {
query: $("#search_query").val(),
top_k: top_k,
similarity: similarity,
start_date: $("#start_date").val(),
end_date: $("#end_date").val(),
};
data = JSON.stringify(data);
console.log(data);
$.ajax({
url: "/semantic_search",
type: "post",
dataType: "json", // 預期從 server 接收的資料型態
contentType: "application/json; charset=utf-8", // 要送到 server 的資料型態
data: data,
})
.done(function (data) {
data = JSON.parse(data["semantic_search"]);
$("#results").empty();
$("#results").append(`
title |
content_tags |
descriptionFilterHtml |
masterUnit |
startDate |
endDate |
`);
$.each(data, function (indexInArray, valueOfElement) {
$("tbody").append(`
${valueOfElement.title} |
${
valueOfElement.content_tags.slice(0, 10) + "..."
}
${valueOfElement.content_tags}
|
${
valueOfElement.descriptionFilterHtml.slice(0, 50) + "..."
}
${valueOfElement.descriptionFilterHtml}
|
${valueOfElement.masterUnit} |
${valueOfElement.startDate} |
${valueOfElement.endDate} |
`);
});
$("table").DataTable({
order: [],
});
})
.fail(function () {
console.log("error");
});
$.ajax({
url: "/wordcloud",
type: "post",
dataType: "json", // 預期從server接收的資料型態
contentType: "application/json; charset=utf-8", // 要送到server的資料型態
data: data,
})
.done(function (data) {
data = JSON.parse(data["wordcloud"]);
$("#wordcloud").empty();
$("#wordcloud").append(data);
})
.fail(function () {
console.log("error");
});
$.ajax({
url: "/tag_list",
type: "post",
dataType: "json", // 預期從server接收的資料型態
contentType: "application/json; charset=utf-8", // 要送到server的資料型態
data: data,
})
.done(function (data) {
data = JSON.parse(data["tag_list"]);
$("#tag_list").empty();
if (data.length != 0) {
$("#tag_list").append(`Top 100 Tags
`);
$.each(data, function (indexInArray, valueOfElement) {
$("#tag_list").append(`
#${valueOfElement}
`);
});
}
})
.fail(function () {
console.log("tag list error");
});
$.ajax({
url: "/add_search",
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: data
})
.done(function(data){
let max_add = 5
console.log(data);
add_data = data["add_keywords"]
console.log(add_data)
let contentList = [];
let falseCount = 0;
let len = Math.min(max_add, add_data.length);
for (i=0; i
`);
}
add_data = data["add_labels"]
console.log(data)
contentList = [];
falseCount = 0;
len = Math.min(max_add, add_data.length);
for (i=0; i
`);
}
add_data = data["add_names"]
console.log(data)
contentList = [];
falseCount = 0;
len = Math.min(max_add, add_data.length);
for (i=0; i
`);
}
})
.fail(function(){
console.log("add search error")
});
}
}
$("#search").on("click", function () {
semantic_search();
});
$("#search_query").on("keyup", function (event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
$("#search").click();
}
});
// 延遲取得輸入值
function delayInput(fn, ms) {
let timer = 0;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(fn.bind(this, ...args), ms || 0);
};
}
// $("#search_query").keyup(
// delayInput(function () {
// console.log("Time elapsed!", this.value);
// getSearchData(this.value);
// }, 500)
// );
function getSearchData(params) {
$.ajax({
url: "static/data.json",
type: "get",
dataType: "json",
contentType: "application/json; charset=utf-8",
})
.done((data) => {
let contentList = [];
let falseCount = 0;
data.map((e) => {
if (params !== "" && e.inputValue === params) {
e.data.map((item) => {
let content = `
`;
contentList.push(content);
});
} else {
// 錯誤計數
falseCount++;
}
// 無符合結果則清空
if (falseCount === e.data.length) {
contentList = [];
$(".add_search").html(contentList);
} else {
$(".add_search").html(contentList);
}
});
})
.fail((err) => {
console.log("add search error", err);
});
}
function addValue(params) {
document.getElementById("search_query").value =
document.getElementById("search_query").value + ` ${params}`;
//$("#search").click();
}