### Install Sphinx as a Windows Service Source: https://context7.com/sphinxsearch/sphinx/llms.txt Installs the Sphinx search daemon as a Windows service. Specify the configuration file and a desired service name. ```bash searchd --install --config C:\Sphinx\sphinx.conf --servicename SphinxSearch ``` -------------------------------- ### Start Sphinx Daemon with I/O Statistics Logging Source: https://context7.com/sphinxsearch/sphinx/llms.txt Use this command to start the Sphinx search daemon while enabling logging for I/O statistics. This is useful for monitoring disk and network activity. ```bash searchd --iostats ``` -------------------------------- ### Example Hit List Data Transformation Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-index-format.txt Example values demonstrating the transformation of raw word positions into compressed byte streams. ```text title = woodchuck chuck content = just how many wood would a woodchuck chuck, if a woodchuck could chuck wood? ``` ```text raw-word-positions = 2, 16777224, 16777229 ``` ```text uncompressed-hitlist = 2, 16777222, 5, 0 ``` ```text compressed-hitlist-bytes = 0x02, 0x88, 0x80, 0x80, 0x06, 0x05, 0x00 ``` -------------------------------- ### Start Sphinx Search Daemon (searchd) Source: https://context7.com/sphinxsearch/sphinx/llms.txt Launches the searchd daemon to serve queries. Use '--config' to specify a configuration file, '--console' for foreground/debug mode, or '--stop' to terminate. ```bash # Start search daemon searchd ``` ```bash # Start with specific config file searchd --config /path/to/sphinx.conf ``` ```bash # Start in console mode (foreground, debug output) searchd --console ``` ```bash # Check daemon status searchd --status ``` ```bash # Stop running daemon searchd --stop ``` ```bash # Start with specific port searchd --port 9312 ``` -------------------------------- ### Start Sphinx Daemon with CPU Statistics Logging Source: https://context7.com/sphinxsearch/sphinx/llms.txt Initiate the Sphinx search daemon with CPU statistics logging enabled. This helps in analyzing the processor load generated by the search daemon. ```bash searchd --cpustats ``` -------------------------------- ### Windows Expat Support Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures Sphinx to compile with libexpat support on Windows. Includes options for dynamic loading and installation of the DLL. ```cmake if (HAVE_expat) option (WITH_EXPAT "compile with libexpat support" ON) if (WITH_EXPAT) set (USE_LIBEXPAT 1) include_directories (${expat_INCLUDE}) CMAKE_DEPENDENT_OPTION (DL_EXPAT "load expat library dynamically" OFF "USE_LIBEXPAT" ON) if (DL_EXPAT) set (DL_EXPAT 1) set (EXPAT_LIB libexpat.dll) else( DL_EXPAT ) list (APPEND EXTRA_LIBRARIES ${expat_LIB}) endif () install (FILES ${expat_ROOT}/libs/libexpat.dll DESTINATION bin COMPONENT APPLICATIONS) endif () endif () ``` -------------------------------- ### Windows iconv Support Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures Sphinx to compile with iconv support on Windows. Includes options for dynamic loading and installation of the DLL. ```cmake if (HAVE_iconv) option (WITH_ICONV "compile with iconv support" ON) if (WITH_ICONV) set (USE_LIBICONV 1) include_directories (${iconv_INCLUDE}) list (APPEND EXTRA_LIBRARIES ${iconv_LIB}) install (FILES ${iconv_ROOT}/bin/iconv.dll DESTINATION bin COMPONENT APPLICATIONS) endif () endif () ``` -------------------------------- ### Windows MySQL Support Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures Sphinx to compile with MySQL support on Windows. Includes options for dynamic loading and installation of the DLL. ```cmake if (HAVE_mysql) option (WITH_MYSQL "compile with mysql support" ON) if (WITH_MYSQL) set (USE_MYSQL 1) include_directories (${mysql_INCLUDE}) CMAKE_DEPENDENT_OPTION (DL_MYSQL "load mysql library dynamically" OFF "USE_MYSQL" ON) if (DL_MYSQL) set (DL_MYSQL 1) set (MYSQL_LIB libmysql.dll) else( DL_MYSQL ) list (APPEND EXTRA_LIBRARIES ${mysql_LIB}) endif () install (FILES ${mysql_ROOT}/bin/libmysql.dll DESTINATION bin COMPONENT APPLICATIONS) endif () endif () ``` -------------------------------- ### Windows PostgreSQL Support Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures Sphinx to compile with PostgreSQL support on Windows. Includes options for dynamic loading and installation of DLLs. ```cmake if (HAVE_pq) option (WITH_PGSQL "compile with pq support" OFF) if (WITH_PGSQL) set (USE_PGSQL 1) include_directories (${pq_INCLUDE}) CMAKE_DEPENDENT_OPTION (DL_PGSQL "load pgsql library dynamically" OFF "USE_PGSQL" ON) if (DL_PGSQL) set (DL_PGSQL 1) set (PGSQL_LIB libpq.dll) else( DL_PGSQL ) list (APPEND EXTRA_LIBRARIES ${pq_LIB}) endif () set (SPHINX_PACKAGE_SUFFIX "${SPHINX_PACKAGE_SUFFIX}-pgsql") IF (CMAKE_EXE_LINKER_FLAGS MATCHES "x64") install (FILES ${pq_ROOT}/bin/libeay32.dll ${pq_ROOT}/bin/libiconv-2.dll ${pq_ROOT}/bin/libintl-8.dll ${pq_ROOT}/bin/libpq.dll ${pq_ROOT}/bin/ssleay32.dll DESTINATION bin COMPONENT APPLICATIONS) else() install (FILES ${pq_ROOT}/bin/libeay32.dll ${pq_ROOT}/bin/libiconv.dll ${pq_ROOT}/bin/libintl.dll ${pq_ROOT}/bin/libpq.dll ${pq_ROOT}/bin/ssleay32.dll DESTINATION bin COMPONENT APPLICATIONS) endif () endif () endif () ``` -------------------------------- ### Interact with Sphinx HTTP API Source: https://context7.com/sphinxsearch/sphinx/llms.txt Examples of using curl to perform searches, execute SphinxQL queries, and modify real-time indexes via the HTTP interface. ```bash # Simple search endpoint curl -X POST 'http://localhost:9308/search' \ -d 'index=products&match=@title laptop&select=id,title,price&limit=10' # SQL endpoint - execute SphinxQL queries curl -X POST 'http://localhost:9308/sql' \ -d "query=SELECT id, title, price FROM products WHERE MATCH('laptop') ORDER BY price DESC LIMIT 10" # Search with grouping curl -X POST 'http://localhost:9308/search' \ -d 'index=products&match=laptop&group=category_id&order=@count DESC&limit=5' # Search with filters (via SQL endpoint) curl -X POST 'http://localhost:9308/sql' \ -d "query=SELECT * FROM products WHERE MATCH('laptop') AND price BETWEEN 500 AND 1000 AND category_id IN (1,2,3)" # Insert into RT index curl -X POST 'http://localhost:9308/sql' \ -d "query=INSERT INTO rt_products (id, title, content, price) VALUES (1, 'New Laptop', 'Great laptop', 999)" # Response format (JSON): # { # "attrs": ["id", "title", "price"], # "matches": [ # {"id": 123, "weight": 2500, "attrs": [123, "Gaming Laptop", 1299]}, # {"id": 456, "weight": 2100, "attrs": [456, "Business Laptop", 899]} # ], # "meta": { # "total": 150, # "total_found": 150, # "time": 0.005, # "words": [{"word": "laptop", "docs": 150, "hits": 312}] # } # } ``` -------------------------------- ### Field Start and End Anchors Source: https://context7.com/sphinxsearch/sphinx/llms.txt Anchoring search terms to the beginning or end of a field. ```text ^hello # Word at field start world$ # Word at field end ^"hello world"$ # Phrase spanning entire field ``` -------------------------------- ### Remove Sphinx Service on Windows Source: https://context7.com/sphinxsearch/sphinx/llms.txt Removes a previously installed Sphinx search daemon service from a Windows system. Use the same service name provided during installation. ```bash searchd --delete --servicename SphinxSearch ``` -------------------------------- ### Begin Transaction for Batching RT Operations Source: https://context7.com/sphinxsearch/sphinx/llms.txt Start a transaction in SphinxQL to group multiple real-time index operations (like inserts) into a single atomic unit. Use COMMIT to finalize or ROLLBACK to cancel. ```sql BEGIN; INSERT INTO rt_index VALUES (100, 'Doc 100', 'Content', 1); INSERT INTO rt_index VALUES (101, 'Doc 101', 'Content', 1); INSERT INTO rt_index VALUES (102, 'Doc 102', 'Content', 1); COMMIT; ``` -------------------------------- ### Perform Search with C API Source: https://context7.com/sphinxsearch/sphinx/llms.txt Demonstrates initializing a client, configuring search parameters, executing a query, and processing results using libsphinxclient. ```c #include #include #include int main() { sphinx_client *client; sphinx_result *result; int i, j; // Create client client = sphinx_create(SPH_TRUE); if (!client) { fprintf(stderr, "Failed to create Sphinx client\n"); return 1; } // Configure connection sphinx_set_server(client, "localhost", 9312); sphinx_set_connect_timeout(client, 5.0f); // Configure search sphinx_set_match_mode(client, SPH_MATCH_EXTENDED2); sphinx_set_ranking_mode(client, SPH_RANK_PROXIMITY_BM25, NULL); sphinx_set_limits(client, 0, 20, 1000, 0); // Set sort mode sphinx_set_sort_mode(client, SPH_SORT_EXTENDED, "@weight DESC"); // Add filter sphinx_int64_t values[] = {1, 2, 3}; sphinx_add_filter(client, "category_id", 3, values, SPH_FALSE); // Add range filter sphinx_add_filter_range(client, "price", 100, 500, SPH_FALSE); // Execute query result = sphinx_query(client, "@title laptop", "products", NULL); if (!result) { fprintf(stderr, "Query failed: %s\n", sphinx_error(client)); sphinx_destroy(client); return 1; } // Check for warnings if (result->warning) { printf("Warning: %s\n", result->warning); } // Print results printf("Found %d of %d matches in %.3f sec\n", result->total, result->total_found, result->time); // Print word statistics for (i = 0; i < result->num_words; i++) { printf("Keyword '%s': %d docs, %d hits\n", result->words[i].word, result->words[i].docs, result->words[i].hits); } // Iterate through matches for (i = 0; i < result->num_matches; i++) { printf("Doc ID: %lld, Weight: %d\n", (long long)result->ids[i], result->weights[i]); // Access attributes for (j = 0; j < result->num_attrs; j++) { printf(" %s: ", result->attr_names[j]); switch (result->attr_types[j]) { case SPH_ATTR_INTEGER: case SPH_ATTR_TIMESTAMP: printf("%u", sphinx_get_int(result, i, j)); break; case SPH_ATTR_FLOAT: printf("%.2f", sphinx_get_float(result, i, j)); break; case SPH_ATTR_BIGINT: printf("%lld", (long long)sphinx_get_int(result, i, j)); break; } printf("\n"); } } // Build excerpts const char *docs[] = { "First document about laptops", "Second document about gaming" }; sphinx_excerpt_options opts; sphinx_init_excerpt_options(&opts); opts.before_match = ""; opts.after_match = ""; opts.limit = 200; char **excerpts = sphinx_build_excerpts(client, 2, docs, "products", "laptop", &opts); if (excerpts) { for (i = 0; i < 2; i++) { printf("Excerpt %d: %s\n", i+1, excerpts[i]); free(excerpts[i]); } free(excerpts); } // Cleanup sphinx_destroy(client); return 0; } // Compile with: gcc -o search search.c -lsphinxclient ``` -------------------------------- ### Project Initialization Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Initializes the Sphinx search project and sets up CMake module paths. ```cmake PROJECT (SPHINXSEARCH) SET (SPHINXSEARCH_CMAKE_DIR "${SPHINXSEARCH_SOURCE_DIR}/cmake") SET (CMAKE_MODULE_PATH "${SPHINXSEARCH_SOURCE_DIR}/cmake") SET (EXTRA_LIBRARIES) SET (BANNER) list (APPEND BANNER "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") ``` -------------------------------- ### Build All Sphinx Indexes Source: https://context7.com/sphinxsearch/sphinx/llms.txt Use the 'indexer --all' command to build all indexes defined in the configuration file. Use '--rotate' for seamless updates while searchd is running. ```bash # Build all configured indexes indexer --all ``` ```bash # Build specific index indexer myindex ``` ```bash # Rotate indexes (for seamless updates while searchd is running) indexer --rotate myindex ``` ```bash # Rebuild all indexes with rotation indexer --all --rotate ``` ```bash # Merge two indexes (main + delta pattern) indexer --merge main_index delta_index --rotate ``` ```bash # Build with custom config file indexer --config /path/to/sphinx.conf --all ``` ```bash # Quiet mode (suppress progress output) indexer --quiet --all ``` ```bash # Build dictionary for word breaking indexer --buildstops dict.txt 100000 --buildfreqs myindex ``` -------------------------------- ### Updating Attributes Source: https://context7.com/sphinxsearch/sphinx/llms.txt Modify attributes for specified documents in an index. This example updates the 'category_id' for document ID 123. ```ruby client.UpdateAttributes('products', ['category_id'], { 123 => [10] }) ``` -------------------------------- ### Configure Sphinx Client in Ruby Source: https://context7.com/sphinxsearch/sphinx/llms.txt Initializes the Sphinx client and sets basic search parameters like match mode, ranking, and sorting. ```ruby require 'sphinx' # Create client and configure connection client = Sphinx::Client.new client.SetServer('localhost', 9312) client.SetConnectTimeout(5) # Configure search settings client.SetMatchMode(Sphinx::Client::SPH_MATCH_EXTENDED2) client.SetRankingMode(Sphinx::Client::SPH_RANK_PROXIMITY_BM25) client.SetLimits(0, 20, 1000) # offset, limit, max_matches # Set field weights client.SetFieldWeights('title' => 10, 'content' => 1) # Set sorting client.SetSortMode(Sphinx::Client::SPH_SORT_EXTENDED, '@weight DESC, date_added DESC') ``` -------------------------------- ### Sphinx Configuration File (sphinx.conf) Source: https://context7.com/sphinxsearch/sphinx/llms.txt Defines data sources, indexes (disk-based, real-time, distributed), indexer settings, and search daemon (searchd) parameters. Ensure paths and ports are correctly set for your environment. ```ini # Data source configuration - MySQL example source mysource { type = mysql sql_host = localhost sql_user = myuser sql_pass = mypassword sql_db = mydb sql_port = 3306 # Main document query - must return unique document ID as first column sql_query = SELECT id, title, content, author_id, category_id, \ UNIX_TIMESTAMP(date_added) AS date_added \ FROM documents # Define attributes for filtering and sorting sql_attr_uint = author_id sql_attr_uint = category_id sql_attr_timestamp = date_added # Pre-query to set charset sql_query_pre = SET NAMES utf8 } # Plain disk index configuration index myindex { source = mysource path = /var/data/sphinx/myindex # Morphology settings morphology = stem_en, stem_ru # Minimum word length to index min_word_len = 3 # Enable HTML stripping html_strip = 1 # Stopwords file stopwords = /var/data/sphinx/stopwords.txt # Character folding table (UTF-8 default) charset_table = 0..9, A..Z->a..z, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F } # Real-time index configuration index rt_index { type = rt path = /var/data/sphinx/rt_index rt_mem_limit = 256M # Define RT fields rt_field = title rt_field = content # Define RT attributes rt_attr_uint = category_id rt_attr_timestamp = date_added } # Distributed index configuration index dist_index { type = distributed local = myindex agent = server2:9312:remote_index agent = server3:9312:remote_index } # Indexer settings indexer { mem_limit = 256M max_iops = 40 max_iosize = 1048576 } # Search daemon settings searchd { listen = 9312 # SphinxAPI port listen = 9306:mysql41 # SphinxQL port (MySQL protocol) listen = 9308:http # HTTP API port log = /var/log/sphinx/searchd.log query_log = /var/log/sphinx/query.log pid_file = /var/run/sphinx/searchd.pid max_matches = 1000 seamless_rotate = 1 workers = threads binlog_path = /var/data/sphinx/binlog/ } ``` -------------------------------- ### C++ Naming Conventions: Member Variables and Parameters Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-coding-standard.txt Illustrates correct naming for member variables and function parameters using CamelCase and Hungarian notation with single-character type prefixes. ```cpp class SampleInternalClass { int m_iSomething; // right, got both "m_" prefix and "i" typeid char iAnotherthing; // WRONG, bad typeid char, bad capitalization long m_AnotherField; // WRONG, missing typeid char char * m_lpszWtf; // WRONG, typeid must be single char (or "pp") ... /// right void SampleCall ( RuleType_e eRule, char cKey, bool bFlag, char * sArg ); }; ``` -------------------------------- ### C++ Switch Statement Formatting Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-coding-standard.txt Illustrates the recommended formatting for switch statements, including handling short and long statements, and blocks with local variables. ```cpp switch ( tMyCondition ) { case FIRST: iShortStatement = 1; break; case SECOND: iAnotherShortStatement = 2; break; case THIRD: iLongerStatement = 3; DoSomething(); break; case FOURTH: { int iEvenLongerStatementWithLocals = 4; DoSomethingElse(); break; } } ``` -------------------------------- ### Executing a Basic Search Query Source: https://context7.com/sphinxsearch/sphinx/llms.txt Perform a search query with specified keywords and index, then handle potential errors and warnings. ```ruby result = client.Query('@title laptop @content gaming', 'products') if result.nil? puts "Error: #{client.GetLastError}" exit 1 end puts "Warning: #{client.GetLastWarning}" if client.GetLastWarning && !client.GetLastWarning.empty? ``` -------------------------------- ### Configure DL_UNIXODBC Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures dynamic loading of the UnixODBC library if USE_ODBC and HAVE_DL are enabled. ```cmake message (STATUS "ODBC found is ${ODBC_FOUND}") CMAKE_DEPENDENT_OPTION (DL_UNIXODBC "load UnixODBC library dynamically" ON "USE_ODBC;HAVE_DL" OFF) if (DL_UNIXODBC) set (DL_UNIXODBC 1) GET_FILENAME_COMPONENT (UNIXODBC_LIB ${_DUMMY_LIB} NAME) message (STATUS "ODBC will be loaded dynamically in runtime as ${UNIXODBC_LIB}") list (APPEND BANNER "DL_UNIXODBC=ON") else( DL_UNIXODBC ) list (APPEND EXTRA_LIBRARIES ${_DUMMY_LIB}) endif (DL_UNIXODBC) ``` -------------------------------- ### Configure ZLIB Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the ZLIB option using option_menu, adding necessary libraries. ```cmake message (STATUS "Option WITH_ZLIB ${WITH_ZLIB}") option_menu (ZLIB "compile with zlib support" USE_ZLIB EXTRA_LIBRARIES) ``` -------------------------------- ### C++ Enumeration Formatting Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-coding-standard.txt Demonstrates the standard formatting for enumerations in C++. ```cpp enum ESphExample { FIRST = mandatory_value, SECOND, THIRD, FOURTH }; ``` -------------------------------- ### Monitor Server Status and Configuration Source: https://context7.com/sphinxsearch/sphinx/llms.txt Commands to retrieve server metrics, query metadata, and adjust session or global variables. ```sql -- Show server status SHOW STATUS; -- Show status with filter SHOW STATUS LIKE 'query%'; -- Show query metadata SELECT * FROM myindex WHERE MATCH('test'); SHOW META; -- Show meta with filter SHOW META LIKE 'total%'; -- Show warnings from last query SHOW WARNINGS; -- Show session variables SHOW VARIABLES; -- Show threading info SHOW THREADS; -- Show query execution profile SET profiling=1; SELECT * FROM myindex WHERE MATCH('test query'); SHOW PROFILE; -- Show query execution plan SELECT * FROM myindex WHERE MATCH('complex query'); SHOW PLAN; -- Set session variable SET GLOBAL query_log_format = 'sphinxql'; SET autocommit = 0; -- Set user variable for filtering SET GLOBAL @mylist = (1, 2, 3, 4, 5); SELECT * FROM myindex WHERE category_id IN @mylist; -- Configure query cache SET GLOBAL qcache_max_bytes = 134217728; SET GLOBAL qcache_thresh_msec = 1000; SET GLOBAL qcache_ttl_sec = 300; ``` -------------------------------- ### Server Status and Configuration Source: https://context7.com/sphinxsearch/sphinx/llms.txt Commands to monitor server health, query performance, and session variables. ```APIDOC ## Server Status and Configuration ### Description Commands to retrieve server status, query metadata, execution plans, and modify session/global variables. ### Examples - SHOW STATUS; (Server health) - SHOW META; (Query metadata) - SHOW PLAN; (Execution plan) - SET GLOBAL qcache_max_bytes = 134217728; (Configuration) ``` -------------------------------- ### Configure DL_EXPAT Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures dynamic loading of the expat library if USE_LIBEXPAT and HAVE_DL are enabled. ```cmake CMAKE_DEPENDENT_OPTION (DL_EXPAT "load expat library dynamically" ON "USE_LIBEXPAT;HAVE_DL" OFF) if (DL_EXPAT) set (DL_EXPAT 1) GET_FILENAME_COMPONENT (EXPAT_LIB ${_DUMMY_LIB} NAME) message (STATUS "EXPAT will be loaded dynamically in runtime as ${EXPAT_LIB}") list (APPEND BANNER "DL_EXPAT=ON") else( DL_EXPAT ) list (APPEND EXTRA_LIBRARIES ${_DUMMY_LIB}) endif () ``` -------------------------------- ### Execute Sphinx Search in Java Source: https://context7.com/sphinxsearch/sphinx/llms.txt Demonstrates full lifecycle of a Sphinx search including connection, filtering, sorting, result processing, and excerpt generation. ```java import org.sphx.api.*; import java.util.*; public class SphinxSearchExample { public static void main(String[] args) throws SphinxException { // Create client and configure connection SphinxClient client = new SphinxClient(); client.SetServer("localhost", 9312); client.SetConnectTimeout(5); // Configure search settings client.SetMatchMode(SphinxClient.SPH_MATCH_EXTENDED2); client.SetRankingMode(SphinxClient.SPH_RANK_PROXIMITY_BM25, ""); client.SetLimits(0, 20, 1000, 0); // offset, limit, max_matches, cutoff // Set field weights Map fieldWeights = new LinkedHashMap<>(); fieldWeights.put("title", 10); fieldWeights.put("content", 1); client.SetFieldWeights(fieldWeights); // Set sorting client.SetSortMode(SphinxClient.SPH_SORT_EXTENDED, "@weight DESC, date_added DESC"); // Add filters client.SetFilter("category_id", new long[]{1, 2, 3}, false); // include client.SetFilterRange("price", 100, 500, false); // range filter // Set grouping client.SetGroupBy("category_id", SphinxClient.SPH_GROUPBY_ATTR, "@count DESC"); // Execute search SphinxResult result = client.Query("@title laptop @content gaming", "products"); if (result == null) { System.err.println("Error: " + client.GetLastError()); return; } if (client.GetLastWarning() != null && !client.GetLastWarning().isEmpty()) { System.out.println("Warning: " + client.GetLastWarning()); } // Process results System.out.printf("Found %d of %d matches in %.3f sec%n", result.total, result.totalFound, result.time); // Print keyword statistics for (SphinxWordInfo word : result.words) { System.out.printf("Keyword '%s': %d docs, %d hits%n", word.word, word.docs, word.hits); } // Iterate through matches for (SphinxMatch match : result.matches) { System.out.printf("Doc ID: %d, Weight: %d%n", match.docId, match.weight); // Access attributes for (int i = 0; i < result.attrNames.length; i++) { String attrName = result.attrNames[i]; Object attrValue = match.attrValues.get(i); if (result.attrTypes[i] == SphinxClient.SPH_ATTR_TIMESTAMP) { Date date = new Date(((Long) attrValue) * 1000); System.out.printf(" %s: %s%n", attrName, date); } else if (result.attrTypes[i] == SphinxClient.SPH_ATTR_MULTI) { long[] mva = (long[]) attrValue; System.out.printf(" %s: %s%n", attrName, Arrays.toString(mva)); } else { System.out.printf(" %s: %s%n", attrName, attrValue); } } } // Build excerpts (snippets) String[] docs = {"First document content about laptops", "Second document about gaming laptops"}; String[] excerpts = client.BuildExcerpts(docs, "products", "laptop gaming", new LinkedHashMap() {{ put("before_match", ""); put("after_match", ""); put("chunk_separator", " ... "); put("limit", 200); }}); for (String excerpt : excerpts) { System.out.println("Excerpt: " + excerpt); } // Multi-query example (batched queries) client.ResetFilters(); client.ResetGroupBy(); client.SetSortMode(SphinxClient.SPH_SORT_RELEVANCE, ""); client.AddQuery("laptop", "products"); client.SetSortMode(SphinxClient.SPH_SORT_ATTR_DESC, "price"); client.AddQuery("laptop", "products"); client.SetSortMode(SphinxClient.SPH_SORT_ATTR_ASC, "price"); client.AddQuery("laptop", "products"); SphinxResult[] results = client.RunQueries(); for (int i = 0; i < results.length; i++) { System.out.printf("Query %d: %d results%n", i+1, results[i].total); } } } ``` -------------------------------- ### Verify Sphinx Configuration File Syntax Source: https://context7.com/sphinxsearch/sphinx/llms.txt Check the syntax and validity of a Sphinx configuration file using `indextool --checkconfig`. This helps catch errors before applying changes. ```bash indextool --checkconfig ``` -------------------------------- ### Configure MYSQL Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the WITH_MYSQL option for MySQL support, setting paths for includes and libraries. ```cmake # test for MYSQL message (STATUS "Option WITH_MYSQL ${WITH_MYSQL}") option (WITH_MYSQL "compile with MySQL support" ON) set (WITH_MYSQL_INCLUDES "" CACHE PATH "path to MySQL header files") set (WITH_MYSQL_LIBS "" CACHE PATH "path to MySQL library") set (WITH_MYSQL_ROOT "" CACHE PATH "path to the MySQL bundle (where both header and library lives)") ``` -------------------------------- ### Configure EXPAT Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the EXPAT option using option_menu, setting USE_LIBEXPAT if found. ```cmake # test for EXPAT message (STATUS "Option WITH_EXPAT ${WITH_EXPAT}") unset (_DUMMY_LIB) option_menu (EXPAT "compile with libexpat support" USE_LIBEXPAT _DUMMY_LIB) ``` -------------------------------- ### Executing Multiple Queries Source: https://context7.com/sphinxsearch/sphinx/llms.txt Prepare and execute multiple search queries sequentially, resetting filters and setting different sort modes for each query. ```ruby client.ResetFilters client.ResetGroupBy client.SetSortMode(Sphinx::Client::SPH_SORT_RELEVANCE) client.AddQuery('laptop', 'products') client.SetSortMode(Sphinx::Client::SPH_SORT_ATTR_DESC, 'price') client.AddQuery('laptop', 'products') results = client.RunQueries results.each_with_index do |res, i| puts "Query #{i+1}: #{res['total']} results" end ``` -------------------------------- ### Persistent Connection Management Source: https://context7.com/sphinxsearch/sphinx/llms.txt Demonstrates opening a persistent connection to the Sphinx server before executing queries and closing it afterward. ```ruby client.Open # ... multiple queries ... client.Close ``` -------------------------------- ### Configure ICONV Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the ICONV option using option_menu, dependent on EXPAT being enabled. ```cmake # test for ICONV message (STATUS "Option WITH_ICONV ${WITH_ICONV}") if (WITH_EXPAT) option_menu (ICONV "compile with iconv support" USE_LIBICONV EXTRA_LIBRARIES) endif (WITH_EXPAT) ``` -------------------------------- ### Configure libstemmer CMakeLists.txt Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Copies a CMakeLists.txt file to the libstemmer base directory if it does not exist. This is part of integrating the libstemmer library into the build. ```cmake set (STEMMER_BASEDIR "${SPHINXSEARCH_BINARY_DIR}/libstemmer_c") # copy our CMakeLists there if (NOT EXISTS "${STEMMER_BASEDIR}/CMakeLists.txt") ``` -------------------------------- ### Building Search Excerpts (Snippets) Source: https://context7.com/sphinxsearch/sphinx/llms.txt Generate highlighted snippets from documents based on search keywords, with options for formatting and chunking. ```ruby docs = [ 'First document content about laptops', 'Second document about gaming laptops' ] excerpts = client.BuildExcerpts(docs, 'products', 'laptop gaming', { 'before_match' => '', 'after_match' => '', 'chunk_separator' => ' ... ', 'limit' => 200 }) excerpts.each_with_index do |excerpt, i| puts "Excerpt #{i+1}: #{excerpt}" end ``` -------------------------------- ### Configure WITH_RLP Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the WITH_RLP option for RLP library support. Checks for the presence of RLP sources. ```cmake message (STATUS "Option WITH_RLP ${WITH_RLP}") option (WITH_RLP "compile with RLP library support" OFF) if (WITH_RLP) if (EXISTS "${SPHINXSEARCH_SOURCE_DIR}/rlp/rlp/include/bt_rlp_c.h") set (USE_RLP 1) else() message (SEND_ERROR "missing RLP sources from librlp") unset (WITH_RLP CACHE) endif () endif (WITH_RLP) ``` -------------------------------- ### Configure Dynamic or Static MySQL Linking Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures whether to load the MySQL library dynamically or link statically, based on available options. ```cmake CMAKE_DEPENDENT_OPTION (DL_MYSQL "load mysql library dynamically" ON "MYSQL_FOUND;HAVE_DL;NOT STATIC_MYSQL" OFF) CMAKE_DEPENDENT_OPTION (STATIC_MYSQL "link to mysql library statically" OFF "MYSQL_FOUND;NOT DL_MYSQL" OFF) if (STATIC_MYSQL) message (STATUS "Mysql will be linked statically") ``` -------------------------------- ### Find and Configure MySQL Package Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Finds the MySQL package and processes its compiler and linker flags. ```cmake if (WITH_MYSQL) find_package (MYSQL) if (MYSQL_FOUND) set (USE_MYSQL 1) include_directories (${MYSQL_INCLUDE_DIR}) # -DNDEBUG we set or reset on global level, so purge it from myqsl flags string (REPLACE "-DNDEBUG" "" MYSQL_CXXFLAGS "${MYSQL_CXXFLAGS}") # keep only defs, include paths and libs string (REGEX MATCHALL "-[DLIl]([^ ]+)" MYSQL_CXXFLAGS "${MYSQL_CXXFLAGS}") # convert list after MATCHALL back to plain string string (REGEX REPLACE ";" " " MYSQL_CXXFLAGS "${MYSQL_CXXFLAGS}") if (MYSQL_CXXFLAGS) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MYSQL_CXXFLAGS}") endif (MYSQL_CXXFLAGS) endif (MYSQL_FOUND) ``` -------------------------------- ### Configure ODBC Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the ODBC option using option_menu, which sets USE_ODBC if found. ```cmake message (STATUS "Option WITH_ODBC ${WITH_ODBC}") unset (_DUMMY_LIB) option_menu (ODBC "compile with UnixODBC support" USE_ODBC _DUMMY_LIB) ``` -------------------------------- ### Configure USE_SYSLOG Option Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Configures the USE_SYSLOG option, enabling syslog logging if the HAVE_SYSLOG_H header is available. ```cmake message (STATUS "Option USE_SYSLOG ${USE_SYSLOG}") CMAKE_DEPENDENT_OPTION (USE_SYSLOG "compile with possibility to use syslog for logging" ON "HAVE_SYSLOG_H" OFF) IF (USE_SYSLOG) set (USE_SYSLOG 1) else(USE_SYSLOG) unset (USE_SYSLOG) ENDIF (USE_SYSLOG) ``` -------------------------------- ### Dump Sphinx Index Configuration from Header Source: https://context7.com/sphinxsearch/sphinx/llms.txt Extract and display the configuration settings stored within a Sphinx index's header using `indextool --dumpconfig`. ```bash indextool --dumpconfig /var/data/sphinx/myindex.sph ``` -------------------------------- ### Set Compiler Flags and Build Library Source: https://github.com/sphinxsearch/sphinx/blob/master/libre2/CMakeLists.txt Configures compiler flags for GCC and MSVC, defines preprocessor macros, and adds the RE2 static library. Includes specific flags for optimization and debugging. ```cmake include_directories(".") IF(CMAKE_COMPILER_IS_GNUCXX) SET(CMAKE_CXX_FLAGS "-Wall -O3 -g -pthread -Wsign-compare -c ") ELSEIF (MSVC) set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Oi /GL") add_definitions("/D NOMINMAX") ENDIF() add_definitions("-DNDEBUG") add_library ( RE2 STATIC ${SOURCES} ${_HF} ${_IHF} ) ``` -------------------------------- ### Configure RE2 CMakeLists.txt Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Copies a CMakeLists.txt file to the RE2 base directory if it doesn't exist. This is typically used to integrate the RE2 library into the build system. ```cmake # copy our CMakeLists there if (NOT EXISTS "${RE2_BASEDIR}/CMakeLists.txt") message(STATUS "${CMAKE_SOURCE_DIR} - source dir") configure_file ("${CMAKE_SOURCE_DIR}/libre2/CMakeLists.txt" "${RE2_BASEDIR}/CMakeLists.txt" @ONLY) endif () ``` -------------------------------- ### Define Infix Hash Entries and Checkpoints Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-index-format.txt Structure definitions for infix hash entries and checkpoint headers used in index files. ```text infix_entry[] infix_hash_entries checkpoint[] checkpoints checkpoint is: dword keyword_len byte[] keyword [ keyword_len ] qword dict_offset if min_infix_len > 0: tag "infix-blocks" infix_block[] infix_hash_blocks tag "dict-header" zint num_checkpoints zint checkpoints_offset zint infix_codepoint_bytes zint infix_blocks_offset ``` -------------------------------- ### Compiler Version and System Info Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Retrieves and stores the compiler version and system information (uname) on Unix-like systems. ```cmake if ( HAVE_GCC_LIKE ) execute_process (COMMAND "${CMAKE_CXX_COMPILER}" "-dumpversion" OUTPUT_VARIABLE gcc_ver) remove_crlf (COMPILER ${gcc_ver}) message (STATUS "Compiler is ${COMPILER}") endif () message (STATUS "Storing system name") if (UNIX AND NOT CYGWIN) execute_process (COMMAND "uname" "-a" OUTPUT_VARIABLE OS_UNAME) remove_crlf (OS_UNAME ${OS_UNAME}) endif (UNIX AND NOT CYGWIN) ``` -------------------------------- ### Check for Standard Header Files Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Includes the standard header file check module. ```cmake message (STATUS "Checking for standard header files") include (ac_header_stdc) ``` -------------------------------- ### Download and Extract libstemmer Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Downloads and extracts the libstemmer library from a tarball if it's not found. It prioritizes a local tarball and then downloads from a specified URL. ```cmake if (EXISTS "${LIBS_BUNDLE}/libstemmer_c.tgz") message (STATUS "Unpack Stemmer from ${LIBS_BUNDLE}/libstemmer_c.tgz") execute_process ( COMMAND "${CMAKE_COMMAND}" -E tar xfz "${LIBS_BUNDLE}/libstemmer_c.tgz" WORKING_DIRECTORY "${SPHINXSEARCH_BINARY_DIR}") # download from github as zip archive else(EXISTS "${LIBS_BUNDLE}/libstemmer_c.tgz") set (STEMMER_URL "http://snowball.tartarus.org/dist/libstemmer_c.tgz") if (NOT EXISTS "${SPHINXSEARCH_BINARY_DIR}/libstemmer_c.tgz") message (STATUS "Downloading Stemmer") file (DOWNLOAD ${STEMMER_URL} ${SPHINXSEARCH_BINARY_DIR}/libstemmer_c.tgz SHOW_PROGRESS) endif () message (STATUS "Unpack Stemmer from ${SPHINXSEARCH_BINARY_DIR}/libstemmer_c.tgz") execute_process ( COMMAND "${CMAKE_COMMAND}" -E tar xfz "${SPHINXSEARCH_BINARY_DIR}/libstemmer_c.tgz" WORKING_DIRECTORY "${SPHINXSEARCH_BINARY_DIR}") endif (EXISTS "${LIBS_BUNDLE}/libstemmer_c.tgz") ``` -------------------------------- ### Generate Snippets and Keywords Source: https://context7.com/sphinxsearch/sphinx/llms.txt Functions for highlighting search terms in results and extracting keyword statistics. ```sql -- Generate search result snippets (highlighting) CALL SNIPPETS( ('First document text here', 'Second document text'), 'myindex', 'search query', '' AS before_match, '' AS after_match, 100 AS limit ); -- Generate snippets with more options CALL SNIPPETS( 'The quick brown fox jumps over the lazy dog', 'myindex', 'fox dog', '' AS before_match, '' AS after_match, '...' AS chunk_separator, 200 AS limit, 1 AS around, 1 AS use_boundaries ); -- Extract keywords with statistics CALL KEYWORDS('running test query', 'myindex'); -- Keywords with hit statistics CALL KEYWORDS('running test query', 'myindex', 1); -- Query suggestions (did you mean) CALL QSUGGEST('sphnix', 'myindex'); ``` -------------------------------- ### Manage User-Defined Functions Source: https://context7.com/sphinxsearch/sphinx/llms.txt Operations for loading, executing, and removing custom functions from shared libraries. ```sql -- Create UDF from shared library CREATE FUNCTION myudf RETURNS INTEGER SONAME 'myudf.so'; -- Create UDF returning float CREATE FUNCTION myfloatudf RETURNS FLOAT SONAME 'myudf.so'; -- Create UDF returning string CREATE FUNCTION mystringfunc RETURNS STRING SONAME 'myudf.so'; -- Use UDF in query SELECT id, myudf(category_id, price) AS custom_score FROM products WHERE MATCH('laptop') ORDER BY custom_score DESC; -- Drop UDF DROP FUNCTION myudf; -- Show loaded plugins SHOW PLUGINS; -- Reload all plugins RELOAD PLUGINS; ``` -------------------------------- ### Check Sphinx Index for Consistency Errors Source: https://context7.com/sphinxsearch/sphinx/llms.txt Run `indextool --check` to verify the integrity and consistency of a Sphinx index. It reports any detected errors. ```bash indextool --check myindex ``` -------------------------------- ### Check for Library Functions Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Searches for and checks for the availability of various library functions, linking necessary libraries. ```cmake message (STATUS "Checking for library functions") ac_search_libs ("socket" "setsockopt" _DUMMY EXTRA_LIBRARIES) ac_search_libs ("nsl;socket;resolv" "gethostbyname" _DUMMY EXTRA_LIBRARIES) ac_search_libs ("m" "logf" HAVE_LOGF EXTRA_LIBRARIES) ac_search_libs ("dl;dld" "dlopen" HAVE_DL EXTRA_LIBRARIES) ``` ```cmake if (HAVE_DL) set (HAVE_DLOPEN 1) set (HAVE_DLERROR 1) endif (HAVE_DL) ``` -------------------------------- ### Index Management Commands Source: https://context7.com/sphinxsearch/sphinx/llms.txt Commands for managing, describing, and maintaining Sphinx indexes. ```APIDOC ## SQL Index Management ### Description Commands to inspect, modify, and maintain index structures and data. ### Methods SHOW, DESCRIBE, TRUNCATE, FLUSH, OPTIMIZE, ATTACH, ALTER, RELOAD ### Examples - SHOW TABLES; (List all indexes) - DESCRIBE myindex; (Show schema) - TRUNCATE RTINDEX rt_index; (Clear data) - OPTIMIZE INDEX rt_index; (Merge chunks) - ALTER TABLE myindex ADD COLUMN new_attr INTEGER; (Modify schema) ``` -------------------------------- ### Dump Index Header Information Source: https://context7.com/sphinxsearch/sphinx/llms.txt Use `indextool --dumpheader` to display the header information of a Sphinx index. This can be useful for diagnostics. ```bash indextool --dumpheader myindex ``` -------------------------------- ### Check for Specific Header Files Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Checks for the existence of specific header files. ```cmake message (STATUS "Checking for specific headers") ac_check_headers ("execinfo.h;syslog.h;sys/eventfd.h") ``` -------------------------------- ### SphinxQL Real-Time Index Operations Source: https://context7.com/sphinxsearch/sphinx/llms.txt Manage documents in real-time indexes using standard SQL-like commands. ```APIDOC ## INSERT / REPLACE / DELETE / UPDATE [rt_index] ### Description Performs CRUD operations on real-time indexes or updates attributes on existing indexes. ### Method INSERT, REPLACE, DELETE, UPDATE ### Endpoint MySQL protocol port 9306 ### Parameters #### Request Body - **id** (integer) - Required - Unique document identifier. - **attributes** (mixed) - Required - Fields defined in the index schema. ### Response #### Success Response (200) - **status** (string) - Confirmation of the operation success. ``` -------------------------------- ### Clone RE2 Repository with Git Source: https://github.com/sphinxsearch/sphinx/blob/master/CMakeLists.txt Clones the RE2 repository using Git, retrying up to 3 times in case of errors. Requires a Git executable. ```cmake set (RE2_REPO "https://github.com/sphinxsearch/re2.git") message (STATUS "Cloning RE2 from ${RE2_REPO}") # here we have to clone remote libre2 repo # c-p from cmake external project: try to clone 3 times in case of errors. set (error_code 1) set (number_of_tries 0) while (error_code AND number_of_tries LESS 3) execute_process ( COMMAND "${GIT_EXECUTABLE}" clone "${RE2_REPO}" "${RE2_BASEDIR}" WORKING_DIRECTORY "${SPHINXSEARCH_BINARY_DIR}" RESULT_VARIABLE error_code ) math (EXPR number_of_tries "${number_of_tries} + 1") endwhile () if (error_code) message (FATAL_ERROR "Failed to clone repository: ${RE2_REPO}") endif () ``` -------------------------------- ### SphinxQL Search with Relevance Weight Ordering Source: https://context7.com/sphinxsearch/sphinx/llms.txt Execute a full-text search and order the results by relevance weight using the WEIGHT() function and ORDER BY clause in SphinxQL. ```sql SELECT id, WEIGHT() AS w FROM myindex WHERE MATCH('sphinx search') ORDER BY w DESC; ``` -------------------------------- ### SphinxQL Search Options Configuration Source: https://context7.com/sphinxsearch/sphinx/llms.txt Customize search behavior using the OPTION clause in SphinxQL, including ranker selection, maximum matches, field weights, and cutoff values. ```sql SELECT * FROM myindex WHERE MATCH('query') OPTION ranker=bm25, max_matches=5000, field_weights=(title=10, content=1), cutoff=10000; ``` -------------------------------- ### Basic Full-Text Search with SphinxQL Source: https://context7.com/sphinxsearch/sphinx/llms.txt Perform a basic full-text search on a Sphinx index using the MATCH() operator in SphinxQL. Connect to Sphinx via MySQL client on port 9306. ```sql -- Connect using MySQL client -- mysql -h 127.0.0.1 -P 9306 -- Basic full-text search SELECT * FROM myindex WHERE MATCH('search query'); ``` -------------------------------- ### Build Infixes for an Existing Sphinx Index Source: https://context7.com/sphinxsearch/sphinx/llms.txt Generate infixes for an existing Sphinx index using `indextool --build-infixes`. This is typically done for enabling prefix or substring searches. ```bash indextool --build-infixes myindex ``` -------------------------------- ### C++ Naming Conventions: Constants Source: https://github.com/sphinxsearch/sphinx/blob/master/doc/internals-coding-standard.txt Shows the convention for naming constants, which must be in all uppercase, whether typed or defined. ```cpp const bool FAIL_ON_NULL_SOURCE = false; #define READ_NO_SIZE_HINT 0 ``` -------------------------------- ### Setting Filters and Ranges Source: https://context7.com/sphinxsearch/sphinx/llms.txt Use SetFilter, SetFilterRange, and SetFilterFloatRange to include specific values or define numerical ranges for filtering search results. ```ruby client.SetFilter('category_id', [1, 2, 3]) client.SetFilterRange('price', 100, 500) client.SetFilterFloatRange('rating', 3.5, 5.0) ```