cpfo's Hexo Blog

website·Indexed 30 days ago

https://cpfo.github.io/hexoblog/

A technical blog featuring guides on Elasticsearch (including Java High Level REST Client 7.12, DSL queries, and MySQL synchronization), Python programming (IO, multiprocessing, and built-in modules like itertools and argparse), Go language basics, and development tool configurations for macOS and Windows.

Tokens
48.6K
Snippets
281
Records
328
Agent score
72%

What's inside cpfo hexoblog

  1. Compare Algolia Crawler vs Official Hexo Algolia Plugins

    There are two primary ways to populate an Algolia index for a Hexo blog:

    1. Official Plugin Path: Uses plugins like hexo-algolia or hexo-algoliasearch. The flow is: Hexo Articles -> Plugin -> Algolia Index -> NexT Search.
    2. Crawler Path: Uses Algolia Crawler to scrape the deployed site. The flow is: Deployed Hexo Pages -> Algolia Crawler -> Algolia Index -> NexT Search.

    While the Crawler replaces the index generation/upload step, the NexT frontend still requires the appId, Search-only API Key, and indexName to function.

  2. Understand Asynchronous IO vs Synchronous IO

    In a synchronous IO model, the main thread hangs and waits for an IO operation to complete. In an asynchronous IO model, the code issues an IO command and immediately moves on to other tasks without waiting for the result. When the IO operation completes, the CPU is notified to process the result. This requires an event loop (message loop) that continuously reads and processes events, significantly improving the multi-tasking capability of IO-intensive programs.
  3. Understand Python modules and packages

    In Python, a module is any .py file. Modules are used to group functions and variables to improve code maintainability and reuse. To prevent naming conflicts between modules, Python uses Packages, which organize modules into directories. For a directory to be recognized as a package, it must contain an __init__.py file (which can be empty). Packages can be nested to create multi-level hierarchies.

    Warning: Do not name your modules the same as system modules, as this will cause system module imports to fail.

  4. Understand the relationship between asyncio and aiohttp

    While asyncio provides the foundation for single-threaded concurrent I/O operations and implements low-level protocols like TCP, UDP, and SSL, aiohttp is a specialized HTTP framework built on top of asyncio. aiohttp is particularly useful for building high-concurrency web servers where HTTP connections are treated as I/O operations.
  5. Understand Inverted Indexing in Elasticsearch

    Elasticsearch uses an inverted index to enable fast full-text searches, contrasting with the forward indexing used by traditional databases like MySQL.

    • Forward Indexing: Maps a document ID to its content. Searching for a specific term requires a full table scan (scanning every document) to check if the term exists, which is inefficient for large datasets.
    • Inverted Indexing: Maps terms (keywords) to the documents that contain them.
      • Document: The unit of data being searched (e.g., a product or webpage).
      • Term: A meaningful word derived from a document using an analysis/tokenization algorithm.

    Search Process: When a user searches for a phrase, the input is tokenized into terms. Elasticsearch looks up these terms in the inverted index to find the associated document IDs and then retrieves the full documents using those IDs.

  6. Understand the basic structure of a Go program

    A Go source file consists of a package declaration, import statements, functions, variables, statements, expressions, and comments.

    Key rules:

    • Package Declaration: The first non-comment line must define the package. package main defines a standalone executable program.
    • Imports: The import keyword is used to include external packages (e.g., fmt for formatted I/O).
    • Main Function: Every executable must have a func main(), which is the entry point of the program (executed after any init() functions).
    • Exporting (Visibility): Identifiers starting with an uppercase letter (e.g., Print) are exported and accessible from other packages. Identifiers starting with a lowercase letter are private to the package.
    package main
    
    import "fmt"
    
    func main() {
        /* This is a block comment */
        // This is a single-line comment
        fmt.Println("hello, world")
    }
  7. Understand Elasticsearch relevance scoring (BM25)

    Elasticsearch ranks documents based on their relevance to the search terms using a score (_score).

    • TF-IDF: The original algorithm where higher term frequency led to higher scores, which could cause single terms to dominate the result.
    • BM25: The default algorithm since version 5.1. It improves upon TF-IDF by introducing a saturation limit on term frequency, resulting in a smoother scoring curve and more balanced results.
  8. Implement conditional logic and pattern matching

    Use if, elif, and else for standard conditional branching. For complex pattern matching, use the match case syntax (introduced in Python 3.10). match allows matching specific values, multiple values using the | operator, or conditional guards using if within a case statement.
    # Standard if-elif-else
    if condition1:
        # execute 1
    elif condition2:
        # execute 2
    else:
        # execute 4
    
    # Pattern Matching
    age = 15
    match age:
        case x if x < 10:
            print(f'< 10 years old: {x}')
        case 10:
            print('10 years old.')
        case 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18:
            print('11~18 years old.')
        case _:
            print('not sure.')
  9. Handle basic input and output in Python

    Use input() to read a string from the user and print() to output data to the console. When using print(), comma-separated arguments are automatically converted to spaces in the output.
    # Input
    name = input('Please enter name: ')
    
    # Output
    print('hello aa')
    print('hello', 'Zhang San', 'haha') # Outputs: hello Zhang San haha
  10. Handle exceptions using try-except-else-finally

    Python uses try...except...else...finally blocks to handle exceptions. Multiple except blocks can be used to handle different error types. Note that since all exceptions inherit from BaseException, catching a parent class first will prevent subsequent child class except blocks from executing. The else block runs only if no exception occurred, and the finally block always executes regardless of whether an error was raised.
    def error_test(num):
        try:
            print('try...')
            result = 10 / int(num)
            print('结果是 %s' % result)
        except ValueError as ve:
            print('ValueError: ', ve)
        except ZeroDivisionError as zde:
            print('ZeroDivisionError: ', zde)
        else:
            print('无异常')
        finally:
            print('finally...')
    
    error_test('3')
    error_test(2)
    error_test('a')
    error_test('0')