team-durumi / tailwind-aa

tailwind 기반의 AA theme를 만듭니다.
http://aa.durumi.io
1 stars 0 forks source link

[검색] 개선된 검색 결과를 보여주기 위한 시도 (Typesense) #25

Closed woonjjang closed 3 years ago

woonjjang commented 3 years ago

[Contents Params]

[Taxonomy Term]

[Sources Type/Media Type]

방법은 아직 모르겠음. 검색 파라미터부터 확인 CCWSP에서는 이런 검색을 결국 원할 것 같음.

스크린샷 2021-06-21 오후 4 38 16

woonjjang commented 3 years ago

[검색 JS를 사용한다. ]

[검색엔진 elastic-search ]

[검색엔진 typesense-search ]

woonjjang commented 3 years ago
woonjjang commented 3 years ago

[검색 JS를 사용한다. ]

  • 현재버전
  • fuseJS

[검색엔진 elastic-search ]

[검색엔진 typesense-search ]

@suncrud

mozodev commented 3 years ago
{
            "name": "books",
            "fields": [
            {"name": "title", "type": "string" },
            {"name": "author",  "type": "string[]", "facet": true },
            {"name": "ratings", "type": "int32" }
            ],
            "default_sorting_field": "ratings"
        }

모델 설계

woonjjang commented 3 years ago

https://gifted-cray-fb4a47.netlify.app/

woonjjang commented 3 years ago

[진행]

[typesense - server]

woonjjang commented 3 years ago

indexing 하는 js - populateTypesenseIndex.js

require('dotenv').config();

const Typesense = require("typesense");

module.exports = (async () => {
  // Create a client
  const typesense = new Typesense.Client({
    nodes: [
      {
        host: process.env.TYPESENSE_HOST,
        port: process.env.TYPESENSE_PORT,
        protocol: process.env.TYPESENSE_PROTOCOL
      }
    ],
    apiKey: process.env.TYPESENSE_ADMIN_API_KEY
  });

  const schema = {
    name: "items",
    num_documents: 0,
    fields: [
      {
        name: "reference_code",
        type: "string",
        facet: false
      },
      {
        name: "rg",
        type: "string",
        facet: false
      },
      {
        name: "weight",
        type: "string",
        facet: false
      },      
      {
        name: "series",
        type: "string",
        facet: false
      },
      {
        name: "file",
        type: "string",
        facet: false
      },
      {
        name: "file_number",
        type: "string",
        facet: false
      }, 
      {
        name: "local_identifier",
        type: "string",
        facet: false
      },
      {
        name: "date",
        type: "string",
        facet: false
      },                           
      {
        name: "creators",
        type: "string",
        facet: true
      },
      {
        name: "sources",
        type: "string",
        facet: true
      },
      {
        name: "venues",
        type: "string",
        facet: true
      },
      {
        name: "title",
        type: "string",
        facet: false
      },
      {
        name: "description",
        type: "string",
        facet: false
      },
      {
        name: "shotlist",
        type: "string",
        facet: false
      },
      {
        name: "link",
        type: "string",
        facet: false
      },
      {
        name: "components",
        type: "string",
        facet: false
      },                                            
      {
        name: "media_type",
        type: "string",
        facet: true
      },
    ],
  };

  console.log("Populating index in Typesense");

  const items = require("./data/items.json");

  let reindexNeeded = false;
  try {
    const collection = await typesense.collections("items").retrieve();
    console.log("Found existing schema");
    // console.log(JSON.stringify(collection, null, 2));
    if (
      collection.num_documents !== items.length ||
      process.env.FORCE_REINDEX === "true"
    ) {
      console.log("Deleting existing schema");
      reindexNeeded = true;
      await typesense.collections("items").delete();
    }
  } catch (e) {
    reindexNeeded = true;
  }

  if (!reindexNeeded) {
    return true;
  }

  console.log("Creating schema: ");
  console.log(JSON.stringify(schema, null, 2));
  await typesense.collections().create(schema);

  // const collectionRetrieved = await typesense
  //   .collections("items")
  //   .retrieve();
  // console.log("Retrieving created schema: ");
  // console.log(JSON.stringify(collectionRetrieved, null, 2));

  console.log("Adding records: ");

  // Bulk Import

  try {
    const returnData = await typesense
      .collections("items")
      .documents()
      .import(items);
    console.log(returnData);
    console.log("Done indexing.");

    const failedItems = returnData.filter(item => item.success === false);
    if (failedItems.length > 0) {
      throw new Error(
        `Error indexing items ${JSON.stringify(failedItems, null, 2)}`
      );
    }

    return returnData;
  } catch (error) {
    console.log(error);
  }
})();