123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- 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(`
- <div class="spinner-border text-primary" role="status">
- <span class="visually-hidden">Loading...</span>
- </div>`);
- 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(`
- <table class="table table-striped table-hover">
- <thead>
- <tr>
- <th>title</th>
- <th>content_tags</th>
- <th>descriptionFilterHtml</th>
- <th>masterUnit</th>
- <th>startDate</th>
- <th>endDate</th>
- </tr>
- </thead>
- <tbody></tbody>
- </table>`);
- $.each(data, function (indexInArray, valueOfElement) {
- $("tbody").append(`
- <tr>
- <td>${valueOfElement.title}</td>
- <td>
- <details>
- <summary>${
- valueOfElement.content_tags.slice(0, 10) + "..."
- }</summary>
- <p>${valueOfElement.content_tags}</p>
- </details>
- </td>
- <td>
- <details>
- <summary>${
- valueOfElement.descriptionFilterHtml.slice(0, 50) + "..."
- }
- </summary>
- <p>${valueOfElement.descriptionFilterHtml}</p>
- </details>
- </td>
- <td>${valueOfElement.masterUnit}</td>
- <td>${valueOfElement.startDate}</td>
- <td>${valueOfElement.endDate}</td>
- </tr>
- `);
- });
- $("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<br>`);
- $.each(data, function (indexInArray, valueOfElement) {
- $("#tag_list").append(`
- <span class="badge bg-primary">#${valueOfElement} </span>
- `);
- });
- }
- })
- .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<len; i++){
- $("#add_keywords").append(`
- <button type="button" onclick="addValue('${add_data[i]}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
- <div class="d-flex align-items-center">
- <span>+</span> <p class="m-0 pe-1">${add_data[i]}</p>
- </div>
- </button>`);
-
- }
- add_data = data["add_labels"]
- console.log(data)
- contentList = [];
- falseCount = 0;
- len = Math.min(max_add, add_data.length);
- for (i=0; i<len; i++){
- $("#add_labels").append(`
- <button type="button" onclick="addValue('${add_data[i]}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
- <div class="d-flex align-items-center">
- <span>+</span> <p class="m-0 pe-1">${add_data[i]}</p>
- </div>
- </button>`);
-
- }
- add_data = data["add_names"]
- console.log(data)
- contentList = [];
- falseCount = 0;
- len = Math.min(max_add, add_data.length);
- for (i=0; i<len; i++){
- $("#add_names").append(`
- <button type="button" onclick="addValue('${add_data[i]}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
- <div class="d-flex align-items-center">
- <span>+</span> <p class="m-0 pe-1">${add_data[i]}</p>
- </div>
- </button>`);
-
- }
- })
- .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 = `
- <button type="button" onclick="addValue('${item}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
- <div class="d-flex align-items-center">
- <span>+</span> <p class="m-0 pe-1">${item}</p>
- </div>
- </button>`;
- 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();
- }
|