Sphinx is an open source full text search server. As of version 3, at
the moment of writing, it doesn’t come with official packages for
Fedora or Centos. So proper installation requires few manual steps.
In the following steps *sphinx* will be installed as a service, under
its own user and “ordinary” user will be able to run indexer in secure
and simple way.
1. Download tarball
From sphinxsearch.com/downloads/current/ download the latest version.
At the moment of writing it is Sphinx 3.1.1 (612d99f; Oct 17, 2018)
.
Run:
1 |
$ sudo tar xfvz sphinx-3.1.1-612d99f-linux-amd64.tar.gz -C /opt/ |
2. Create sphinx system user searchd
Create a system user without home and login shell:
1 |
$ sudo adduser -U -r -s /usr/sbin/nologin searchd |
3. Create systemd unit file
Save it as /etc/systemd/system/searchd.service:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Unit] Description=Sphinx search engine (searchd) After=network.target [Service] Type=forking PIDFile=/var/run/searchd/searchd.pid User=searchd Group=searchd ExecStart=/opt/sphinx-3.1.1/bin/searchd --config /etc/searchd/sphinx.conf.in [Install] WantedBy=multi-user.target |
4. Create necessary directories and config file
Run:
1 2 |
$ sudo mkdir /var/run/searchd $ sudo chown searchd.searchd /var/run/searchd |
1 2 3 |
$ sudo mkdir -p /var/lib/searchd/data $ sudo chown -R searchd.searchd /var/lib/searchd $ sudo chmod -R u+rw,go-rwx /var/lib/searchd/data |
1 2 3 |
$ sudo mkdir /var/log/searchd $ sudo chown searchd.searchd /var/log/searchd $ sudo chmod -R u+rw,go-rwx /var/log/searchd |
Make sure that /var/run/searchd will be created on reboot
Create file /usr/lib/tmpfiles.d/searchd.conf:
1 |
d /run/searchd 0755 searchd searchd - - |
The /var/run/searchd is directory where sphinx pid will be saved.
Create Sphinx config file
1 |
$ sudo mkdir /etc/searchd |
Save sphinx config file as /etc/searchd/sphinx.conf.in:
Remember to enter correct db credentials for sql_*
params in the
config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# # Sphinx configuration file sample # # WARNING! While this sample file mentions all available options, # it contains (very) short helper descriptions only. Please refer to # doc/sphinx.html for details. # ############################################################################# ## data source definition ############################################################################# source src1 { # data source type. mandatory, no default value # known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc type = mysql ##################################################################### ## SQL settings (for 'mysql' and 'pgsql' types) ##################################################################### # some straightforward parameters for SQL source types sql_host = 127.0.0.1 sql_user = <enter-username> sql_pass = <enter-password> sql_db = sphinx_test sql_port = 3306 # optional, default is 3306 # UNIX socket name # optional, default is empty (reuse client library defaults) # usually '/var/lib/mysql/mysql.sock' on Linux # usually '/tmp/mysql.sock' on FreeBSD # # sql_sock = /tmp/mysql.sock # MySQL specific client connection flags # optional, default is 0 # # mysql_connect_flags = 32 # enable compression # MySQL specific SSL certificate settings # optional, defaults are empty # # mysql_ssl_cert = /etc/ssl/client-cert.pem # mysql_ssl_key = /etc/ssl/client-key.pem # mysql_ssl_ca = /etc/ssl/cacert.pem # MS SQL specific Windows authentication mode flag # MUST be in sync with charset_type index-level setting # optional, default is 0 # # mssql_winauth = 1 # use currently logged on user credentials # ODBC specific DSN (data source name) # mandatory for odbc source type, no default value # # odbc_dsn = DBQ=C:\data;DefaultDir=C:\data;Driver={Microsoft Text Driver (*.txt; *.csv)}; # sql_query = SELECT id, data FROM documents.csv # ODBC and MS SQL specific, per-column buffer sizes # optional, default is auto-detect # # sql_column_buffers = content=12M, comments=1M # pre-query, executed before the main fetch query # multi-value, optional, default is empty list of queries # # sql_query_pre = SET NAMES utf8 # sql_query_pre = SET SESSION query_cache_type=OFF # main document fetch query # mandatory, integer document ID field MUST be the first selected column sql_query = \ SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \ FROM documents # joined/payload field fetch query # joined fields let you avoid (slow) JOIN and GROUP_CONCAT # payload fields let you attach custom per-keyword values (eg. for ranking) # # syntax is FIELD-NAME 'from' ( 'query' | 'payload-query' ); QUERY # joined field QUERY should return 2 columns (docid, text) # payload field QUERY should return 3 columns (docid, keyword, weight) # # REQUIRES that query results are in ascending document ID order! # multi-value, optional, default is empty list of queries # # sql_joined_field = tags from query; SELECT docid, CONCAT('tag',tagid) FROM tags ORDER BY docid ASC # sql_joined_field = wtags from payload-query; SELECT docid, tag, tagweight FROM tags ORDER BY docid ASC # file based field declaration # # content of this field is treated as a file name # and the file gets loaded and indexed in place of a field # # max file size is limited by max_file_field_buffer indexer setting # file IO errors are non-fatal and get reported as warnings # # sql_file_field = content_file_path # range query setup, query that must return min and max ID values # optional, default is empty # # sql_query will need to reference $start and $end boundaries # if using ranged query: # # sql_query = \ # SELECT doc.id, doc.id AS group, doc.title, doc.data \ # FROM documents doc \ # WHERE id>=$start AND id<=$end # # sql_query_range = SELECT MIN(id),MAX(id) FROM documents # range query step # optional, default is 1024 # # sql_range_step = 1000 # unsigned integer attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # optional bit size can be specified, default is 32 # # sql_attr_uint = author_id # sql_attr_uint = forum_id:9 # 9 bits for forum_id sql_attr_uint = group_id # boolean attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # equivalent to sql_attr_uint with 1-bit size # # sql_attr_bool = is_deleted # bigint attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # declares a signed (unlike uint!) 64-bit attribute # # sql_attr_bigint = my_bigint_id # UNIX timestamp attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # similar to integer, but can also be used in date functions # # sql_attr_timestamp = posted_ts # sql_attr_timestamp = last_edited_ts sql_attr_timestamp = date_added # floating point attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # values are stored in single precision, 32-bit IEEE 754 format # # sql_attr_float = lat_radians # sql_attr_float = long_radians # multi-valued attribute (MVA) attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # MVA values are variable length lists of unsigned 32-bit integers # # syntax is ATTR-TYPE ATTR-NAME 'from' SOURCE-TYPE [;QUERY] [;RANGE-QUERY] # ATTR-TYPE is 'uint' or 'timestamp' # SOURCE-TYPE is 'field', 'query', or 'ranged-query' # QUERY is SQL query used to fetch all ( docid, attrvalue ) pairs # RANGE-QUERY is SQL query used to fetch min and max ID values, similar to 'sql_query_range' # # sql_attr_multi = uint tag from query; SELECT docid, tagid FROM tags # sql_attr_multi = uint tag from ranged-query; \ # SELECT docid, tagid FROM tags WHERE id>=$start AND id<=$end; \ # SELECT MIN(docid), MAX(docid) FROM tags # string attribute declaration # multi-value (an arbitrary number of these is allowed), optional # lets you store and retrieve strings # # sql_attr_string = stitle # JSON attribute declaration # multi-value (an arbitrary number of these is allowed), optional # lets you store a JSON document as an (in-memory) attribute for later use # # sql_attr_json = properties # combined field plus attribute declaration (from a single column) # stores column as an attribute, but also indexes it as a full-text field # # sql_field_string = author # post-query, executed on sql_query completion # optional, default is empty # # sql_query_post = # post-index-query, executed on successful indexing completion # optional, default is empty # $maxid expands to max document ID actually fetched from DB # # sql_query_post_index = REPLACE INTO counters ( id, val ) \ # VALUES ( 'max_indexed_id', $maxid ) # ranged query throttling, in milliseconds # optional, default is 0 which means no delay # enforces given delay before each query step sql_ranged_throttle = 0 # kill-list query, fetches the document IDs for kill-list # k-list will suppress matches from preceding indexes in the same query # optional, default is empty # # sql_query_killlist = SELECT id FROM documents WHERE edited>=@last_reindex # columns to unpack on indexer side when indexing # multi-value, optional, default is empty list # # unpack_zlib = zlib_column # unpack_mysqlcompress = compressed_column # unpack_mysqlcompress = compressed_column_2 # maximum unpacked length allowed in MySQL COMPRESS() unpacker # optional, default is 16M # # unpack_mysqlcompress_maxsize = 16M # hook command to run when SQL connection succeeds # optional, default value is empty (do nothing) # # hook_connect = bash sql_connect.sh # hook command to run after (any) SQL range query # it may print out "minid maxid" (w/o quotes) to override the range # optional, default value is empty (do nothing) # # hook_query_range = bash sql_query_range.sh # hook command to run on successful indexing completion # $maxid expands to max document ID actually fetched from DB # optional, default value is empty (do nothing) # # hook_post_index = bash sql_post_index.sh $maxid ##################################################################### ## xmlpipe2 settings ##################################################################### # type = xmlpipe # shell command to invoke xmlpipe stream producer # mandatory # # xmlpipe_command = cat /var/test.xml # xmlpipe2 field declaration # multi-value, optional, default is empty # # xmlpipe_field = subject # xmlpipe_field = content # xmlpipe2 attribute declaration # multi-value, optional, default is empty # all xmlpipe_attr_XXX options are fully similar to sql_attr_XXX # examples: # # xmlpipe_attr_timestamp = published # xmlpipe_attr_uint = author_id # xmlpipe_attr_bool = is_enabled # xmlpipe_attr_float = latitude # xmlpipe_attr_bigint = guid # xmlpipe_attr_multi = tags # xmlpipe_attr_multi_64 = tags64 # xmlpipe_attr_string = title # xmlpipe_attr_json = extra_data # xmlpipe_field_string = content # perform UTF-8 validation, and filter out incorrect codes # avoids XML parser choking on non-UTF-8 documents # optional, default is 0 # # xmlpipe_fixup_utf8 = 1 } # inherited source example # # all the parameters are copied from the parent source, # and may then be overridden in this source definition source src1throttled : src1 { sql_ranged_throttle = 100 } ############################################################################# ## index definition ############################################################################# # local index example # # this is an index which is stored locally in the filesystem # # all indexing-time options (such as morphology and charsets) # are configured per local index index sphinx_test1 { # index type # optional, default is 'plain' # known values are 'plain', 'distributed', and 'rt' (see samples below) # type = plain # document source(s) to index # multi-value, mandatory # document IDs must be globally unique across all sources source = src1 # index files path and file name, without extension # mandatory, path must be writable, extensions will be auto-appended path = /var/lib/searchd/data/sphinx_test1 # document attribute values (docinfo) storage mode # optional, default is 'extern' # known values are 'none', 'extern' and 'inline' docinfo = extern # dictionary type, 'crc' or 'keywords' # crc is faster to index when no substring/wildcards searches are needed # crc with substrings might be faster to search but is much slower to index # (because all substrings are pre-extracted as individual keywords) # keywords is much faster to index with substrings, and index is much (3-10x) smaller # keywords supports wildcards, crc does not, and never will # optional, default is 'keywords' dict = keywords # memory locking for cached data (.spa and .spi), to prevent swapping # optional, default is 0 (do not mlock) # requires searchd to be run from root mlock = 0 # a list of morphology preprocessors to apply # optional, default is empty # # builtin preprocessors are 'none', 'stem_en', 'stem_ru', 'stem_enru', # 'soundex', and 'metaphone'; additional preprocessors available from # libstemmer are 'libstemmer_XXX', where XXX is algorithm code # (see libstemmer_c/libstemmer/modules.txt) # # morphology = stem_en, stem_ru, soundex # morphology = libstemmer_german # morphology = libstemmer_sv morphology = none # minimum word length at which to enable stemming # optional, default is 1 (stem everything) # # min_stemming_len = 1 # stopword files list (space separated) # optional, default is empty # contents are plain text, charset_table and stemming are both applied # # stopwords = /var/data/stopwords.txt # wordforms file, in "mapfrom > mapto" plain text format # optional, default is empty # # wordforms = /var/data/wordforms.txt # tokenizing exceptions file # optional, default is empty # # plain text, case sensitive, space insensitive in map-from part # one "Map Several Words => ToASingleOne" entry per line # # exceptions = /var/data/exceptions.txt # embedded file size limit # optional, default is 16K # # exceptions, wordforms, and stopwords files smaller than this limit # are stored in the index; otherwise, their paths and sizes are stored # # embedded_limit = 16K # minimum indexed word length # default is 1 (index everything) min_word_len = 1 # ignored characters list # optional, default value is empty # # ignore_chars = U+00AD # minimum word prefix length to index # optional, default is 0 (do not index prefixes) # # min_prefix_len = 0 # minimum word infix length to index # optional, default is 0 (do not index infixes) # # min_infix_len = 0 # maximum substring (prefix or infix) length to index # optional, default is 0 (do not limit substring length) # # max_substring_len = 8 # list of fields to limit prefix/infix indexing to # optional, default value is empty (index all fields in prefix/infix mode) # # prefix_fields = filename # infix_fields = url, domain # expand keywords with exact forms and/or stars when searching fit indexes # search-time only, does not affect indexing, can be 0 or 1 # optional, default is 0 (do not expand keywords) # # expand_keywords = 1 # n-gram length to index, for CJK indexing # only supports 0 and 1 for now, other lengths to be implemented # optional, default is 0 (disable n-grams) # # ngram_len = 1 # n-gram characters list, for CJK indexing # optional, default is empty # # ngram_chars = U+3000..U+2FA1F # phrase boundary characters list # optional, default is empty # # phrase_boundary = ., ?, !, U+2026 # horizontal ellipsis # phrase boundary word position increment # optional, default is 0 # # phrase_boundary_step = 100 # blended characters list # blended chars are indexed both as separators and valid characters # for instance, AT&T will results in 3 tokens ("at", "t", and "at&t") # optional, default is empty # # blend_chars = +, &, U+23 # blended token indexing mode # a comma separated list of blended token indexing variants # known variants are trim_none, trim_head, trim_tail, trim_both, skip_pure # optional, default is trim_none # # blend_mode = trim_tail, skip_pure # whether to strip HTML tags from incoming documents # known values are 0 (do not strip) and 1 (do strip) # optional, default is 0 html_strip = 0 # what HTML attributes to index if stripping HTML # optional, default is empty (do not index anything) # # html_index_attrs = img=alt,title; a=title; # what HTML elements contents to strip # optional, default is empty (do not strip element contents) # # html_remove_elements = style, script # whether to preopen index data files on startup # optional, default is 0 (do not preopen), searchd-only # # preopen = 1 # whether to enable in-place inversion (2x less disk, 90-95% speed) # optional, default is 0 (use separate temporary files), indexer-only # # inplace_enable = 1 # in-place fine-tuning options # optional, defaults are listed below # # inplace_hit_gap = 0 # preallocated hitlist gap size # inplace_docinfo_gap = 0 # preallocated docinfo gap size # inplace_reloc_factor = 0.1 # relocation buffer size within arena # inplace_write_factor = 0.1 # write buffer size within arena # whether to index original keywords along with stemmed versions # enables "=exactform" operator to work # optional, default is 0 # # index_exact_words = 1 # position increment on overshort (less that min_word_len) words # optional, allowed values are 0 and 1, default is 1 # # overshort_step = 1 # position increment on stopword # optional, allowed values are 0 and 1, default is 1 # # stopword_step = 1 # hitless words list # positions for these keywords will not be stored in the index # optional, allowed values are 'all', or a list file name # # hitless_words = all # hitless_words = hitless.txt # detect and index sentence and paragraph boundaries # required for the SENTENCE and PARAGRAPH operators to work # optional, allowed values are 0 and 1, default is 0 # # index_sp = 1 # index zones, delimited by HTML/XML tags # a comma separated list of tags and wildcards # required for the ZONE operator to work # optional, default is empty string (do not index zones) # # index_zones = title, h*, th # index per-document and average per-index field lengths, in tokens # required for the BM25A(), BM25F() in expression ranker # optional, default is 0 (do not index field lenghts) # # index_field_lengths = 1 # regular expressions (regexps) to filter the fields and queries with # gets applied to data source fields when indexing # gets applied to search queries when searching # multi-value, optional, default is empty list of regexps # # regexp_filter = \b(\d+)\" => \1inch # regexp_filter = (blue|red) => color # list of the words considered frequent with respect to bigram indexing # optional, default is empty # # bigram_freq_words = the, a, i, you, my # bigram indexing mode # known values are none, all, first_freq, both_freq # option, default is none (do not index bigrams) # # bigram_index = both_freq # snippet document file name prefix # preprended to file names when generating snippets using load_files option # WARNING, this is a prefix (not a path), trailing slash matters! # optional, default is empty # # snippets_file_prefix = /mnt/mydocs/server1 # whether to apply stopwords before or after stemming # optional, default is 0 (apply stopwords after stemming) # # stopwords_unstemmed = 0 # path to a global (cluster-wide) keyword IDFs file # optional, default is empty (use local IDFs) # # global_idf = /usr/local/sphinx/var/global.idf } # inherited index example # # all the parameters are copied from the parent index, # and may then be overridden in this index definition index sphinx_test1stemmed : sphinx_test1 { path = /var/lib/searchd/data/sphinx_test1stemmed morphology = stem_en } # distributed index example # # this is a virtual index which can NOT be directly indexed, # and only contains references to other local and/or remote indexes index sphinx_dist1 { # 'distributed' index type MUST be specified type = distributed # local index to be searched # there can be many local indexes configured local = sphinx_test1 local = sphinx_test1stemmed # remote agent # multiple remote agents may be specified # syntax for TCP connections is 'hostname:port:index1,[index2[,...]]' # syntax for local UNIX connections is '/path/to/socket:index1,[index2[,...]]' agent = localhost:9313:remote1 agent = localhost:9314:remote2,remote3 # agent = /var/run/searchd.sock:remote4 # remote agent mirrors groups, aka mirrors, aka HA agents # defines 2 or more interchangeable mirrors for a given index part # # agent = server3:9312 | server4:9312 :indexchunk2 # agent = server3:9312:chunk2server3 | server4:9312:chunk2server4 # agent = server3:chunk2server3 | server4:chunk2server4 # agent = server21|server22|server23:chunk2 # blackhole remote agent, for debugging/testing # network errors and search results will be ignored # # agent_blackhole = testbox:9312:testindex1,testindex2 # persistenly connected remote agent # reduces connect() pressure, requires that workers IS threads # # agent_persistent = testbox:9312:testindex1,testindex2 # remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec agent_connect_timeout = 1000 # remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec agent_query_timeout = 3000 # HA mirror agent strategy # optional, defaults to ??? (random mirror) # know values are nodeads, noerrors, roundrobin, nodeadstm, noerrorstm # # ha_strategy = nodeads } # realtime index example # # you can run INSERT, REPLACE, and DELETE on this index on the fly # using MySQL protocol (see 'listen' directive below) index rt { # 'rt' index type must be specified to use RT index type = rt # index files path and file name, without extension # mandatory, path must be writable, extensions will be auto-appended path = /varlib/searchd/data/rt # RAM chunk size limit # RT index will keep at most this much data in RAM, then flush to disk # optional, default is 128M # # rt_mem_limit = 512M # full-text field declaration # multi-value, mandatory rt_field = title rt_field = content # unsigned integer attribute declaration # multi-value (an arbitrary number of attributes is allowed), optional # declares an unsigned 32-bit attribute rt_attr_uint = gid # RT indexes currently support the following attribute types: # uint, bigint, float, timestamp, string, mva, mva64, json # # rt_attr_bigint = guid # rt_attr_float = gpa # rt_attr_timestamp = ts_added # rt_attr_string = author # rt_attr_multi = tags # rt_attr_multi_64 = tags64 # rt_attr_json = extra_data } ############################################################################# ## indexer settings ############################################################################# indexer { # memory limit, in bytes, kiloytes (16384K) or megabytes (256M) # optional, default is 128M, max is 2047M, recommended is 256M to 1024M mem_limit = 128M # maximum IO calls per second (for I/O throttling) # optional, default is 0 (unlimited) # # max_iops = 40 # maximum IO call size, bytes (for I/O throttling) # optional, default is 0 (unlimited) # # max_iosize = 1048576 # maximum xmlpipe2 field length, bytes # optional, default is 2M # # max_xmlpipe2_field = 4M # write buffer size, bytes # several (currently up to 4) buffers will be allocated # write buffers are allocated in addition to mem_limit # optional, default is 1M # # write_buffer = 1M # maximum file field adaptive buffer size # optional, default is 8M, minimum is 1M # # max_file_field_buffer = 32M # how to handle IO errors in file fields # known values are 'ignore_field', 'skip_document', and 'fail_index' # optional, default is 'ignore_field' # # on_file_field_error = skip_document # lemmatizer cache size # improves the indexing time when the lemmatization is enabled # optional, default is 256K # # lemmatizer_cache = 512M } ############################################################################# ## searchd settings ############################################################################# searchd { # [hostname:]port[:protocol], or /unix/socket/path to listen on # known protocols are 'sphinx' (SphinxAPI) and 'mysql41' (SphinxQL) # # multi-value, multiple listen points are allowed # optional, defaults are 9312:sphinx and 9306:mysql41, as below # # listen = 127.0.0.1 # listen = 192.168.0.1:9312 # listen = 9312 # listen = /var/run/searchd.sock listen = 9312 listen = 9306:mysql41 # log file, searchd run info is logged here # optional, default is 'searchd.log' log = /var/log/searchd/searchd.log # query log file, all search queries are logged here # optional, default is empty (do not log queries) query_log = /var/log/searchd/query.log # client read timeout, seconds # optional, default is 5 read_timeout = 5 # request timeout, seconds # optional, default is 5 minutes client_timeout = 300 # maximum amount of children to fork (concurrent searches to run) # optional, default is 0 (unlimited) max_children = 30 # maximum amount of persistent connections from this master to each agent host # optional, but necessary if you use agent_persistent. It is reasonable to set the value # as max_children, or less on the agent's hosts. persistent_connections_limit = 30 # PID file, searchd process ID file name # mandatory pid_file = /var/run/searchd/searchd.pid # seamless rotate, prevents rotate stalls if precaching huge datasets # optional, default is 1 seamless_rotate = 1 # whether to forcibly preopen all indexes on startup # optional, default is 1 (preopen everything) preopen_indexes = 1 # whether to unlink .old index copies on succesful rotation. # optional, default is 1 (do unlink) unlink_old = 1 # attribute updates periodic flush timeout, seconds # updates will be automatically dumped to disk this frequently # optional, default is 0 (disable periodic flush) # # attr_flush_period = 900 # MVA updates pool size # shared between all instances of searchd, disables attr flushes! # optional, default size is 1M mva_updates_pool = 1M # max allowed network packet size # limits both query packets from clients, and responses from agents # optional, default size is 8M max_packet_size = 8M # max allowed per-query filter count # optional, default is 256 max_filters = 256 # max allowed per-filter values count # optional, default is 4096 max_filter_values = 4096 # socket listen queue length # optional, default is 5 # # listen_backlog = 5 # per-keyword read buffer size # optional, default is 256K # # read_buffer = 256K # unhinted read size (currently used when reading hits) # optional, default is 32K # # read_unhinted = 32K # max allowed per-batch query count (aka multi-query count) # optional, default is 32 max_batch_queries = 32 # max common subtree document cache size, per-query # optional, default is 0 (disable subtree optimization) # # subtree_docs_cache = 4M # max common subtree hit cache size, per-query # optional, default is 0 (disable subtree optimization) # # subtree_hits_cache = 8M # multi-processing mode (MPM) # known values are none, fork, prefork, and threads # threads is required for RT backend to work # optional, default is threads workers = threads # for RT to work # max threads to create for searching local parts of a distributed index # optional, default is 0, which means disable multi-threaded searching # should work with all MPMs (ie. does NOT require workers=threads) # # dist_threads = 4 # binlog files path; use empty string to disable binlog # optional, default is build-time configured data directory # binlog_path = # disable logging # binlog_path = /var/data # binlog.001 etc will be created there # binlog flush/sync mode # 0 means flush and sync every second # 1 means flush and sync every transaction # 2 means flush every transaction, sync every second # optional, default is 2 # # binlog_flush = 2 # binlog per-file size limit # optional, default is 128M, 0 means no limit # # binlog_max_log_size = 256M # per-thread stack size, only affects workers=threads mode # optional, default is 64K # # thread_stack = 128K # per-keyword expansion limit (for dict=keywords prefix searches) # optional, default is 0 (no limit) # # expansion_limit = 1000 # RT RAM chunks flush period # optional, default is 0 (no periodic flush) # # rt_flush_period = 900 # query log file format # optional, known values are plain and sphinxql, default is plain # # query_log_format = sphinxql # version string returned to MySQL network protocol clients # optional, default is empty (use Sphinx version) # # mysql_version_string = 5.0.37 # default server-wide collation # optional, default is libc_ci # # collation_server = utf8_general_ci # server-wide locale for libc based collations # optional, default is C # # collation_libc_locale = ru_RU.UTF-8 # threaded server watchdog (only used in workers=threads mode) # optional, values are 0 and 1, default is 1 (watchdog on) # # watchdog = 1 # costs for max_predicted_time model, in (imaginary) nanoseconds # optional, default is "doc=64, hit=48, skip=2048, match=64" # # predicted_time_costs = doc=64, hit=48, skip=2048, match=64 # current SphinxQL state (uservars etc) serialization path # optional, default is none (do not serialize SphinxQL state) # # sphinxql_state = sphinxvars.sql # maximum RT merge thread IO calls per second, and per-call IO size # useful for throttling (the background) OPTIMIZE INDEX impact # optional, default is 0 (unlimited) # # rt_merge_iops = 40 # rt_merge_maxiosize = 1M # interval between agent mirror pings, in milliseconds # 0 means disable pings # optional, default is 1000 # # ha_ping_interval = 0 # agent mirror statistics window size, in seconds # stats older than the window size (karma) are retired # that is, they will not affect master choice of agents in any way # optional, default is 60 seconds # # ha_period_karma = 60 # delay between preforked children restarts on rotation, in milliseconds # optional, default is 0 (no delay) # # prefork_rotation_throttle = 100 # a prefix to prepend to the local file names when creating snippets # with load_files and/or load_files_scatter options # optional, default is empty # # snippets_file_prefix = /mnt/common/server1/ } ############################################################################# ## common settings ############################################################################# common { # lemmatizer dictionaries base path # optional, defaut is /usr/local/share (see ./configure --datadir) # # lemmatizer_base = /usr/local/share/sphinx/dicts # how to handle syntax errors in JSON attributes # known values are 'ignore_attr' and 'fail_index' # optional, default is 'ignore_attr' # # on_json_attr_error = fail_index # whether to auto-convert numeric values from strings in JSON attributes # with auto-conversion, string value with actually numeric data # (as in {"key":"12345"}) gets stored as a number, rather than string # optional, allowed values are 0 and 1, default is 0 (do not convert) # # json_autoconv_numbers = 1 # whether and how to auto-convert key names in JSON attributes # known value is 'lowercase' # optional, default is unspecified (do nothing) # # json_autoconv_keynames = lowercase # trusted plugin directory # optional, default is empty (disable UDFs) # # plugin_dir = /usr/local/sphinx/lib } # --eof-- |
Change config file owner and permission:
1 2 |
$ sudo chown searchd.searchd /etc/searchd/sphinx.conf.in $ sudo chmod 600 /etc/searchd/sphinx.conf.in |
The config file will contain your database credentials – so better to be
safe then sorry.
5. Start searchd daemon
In case you have SELinux enabled, run:
1 |
$ sudo chcon --user=system_u --role=object_r --type=systemd_unit_file_t searchd.service |
Run:
1 2 3 |
$ sudo systemctl daemon-reload $ sudo systemctl enable searchd.service $ sudo systemctl start searchd.service |
Check searchd daemon
1 |
$ systemctl status searchd.service |
And try also:
1 |
$ mysql -h127.0.0.1 -P9306 |
If mysql client made a successful connection – we are good for now.
6. Import test db and run a SphinxQL query
Import this sql file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
CREATE DATABASE sphinx_test DEFAULT CHARSET utf8 DEFAULT COLLATE utf8_general_ci; CREATE TABLE sphinx_test.documents ( id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, group_id INTEGER NOT NULL, group_id2 INTEGER NOT NULL, date_added DATETIME NOT NULL, title VARCHAR(255) NOT NULL, content TEXT NOT NULL ); REPLACE INTO sphinx_test.documents (id, group_id, group_id2, date_added, title, content) VALUES (1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.'), (2, 1, 6, NOW(), 'test two', 'this is my test document number two'), (3, 2, 7, NOW(), 'another doc', 'this is another group'), (4, 2, 8, NOW(), 'doc number four', 'this is to test groups'); CREATE TABLE sphinx_test.tags ( docid INTEGER NOT NULL, tagid INTEGER NOT NULL, UNIQUE (docid, tagid) ); INSERT INTO sphinx_test.tags VALUES (1, 1), (1, 3), (1, 5), (1, 7), (2, 6), (2, 4), (2, 2), (3, 15), (4, 7), (4, 40); |
The sql file is based on /opt/sphinx-3.1.1/etc/example.sql
7. Enable non-root and non-searchd user to run indexer
Edit /etc/sudoers. Add line:
1 |
<yourusername> ALL=(searchd) NOPASSWD: /opt/sphinx-3.1.1/bin/indexer --rotate --all --config /etc/searchd/sphinx.conf.in |
And then as “ordinary” user run:
1 |
$ sudo -u searchd /opt/sphinx-3.1.1/bin/indexer --rotate --all --config /etc/searchd/sphinx.conf.in |
The indexer will execute without the need to enter a password.
In the case of error: sql_connect: failed to load libmysqlclient check:
- is
/usr/lib64/mysql in
/etc/ld.so.conf
(or included files), or add it to the - LD_LIBRARY_PATHis there a symlink /usr/lib64/mysql/libmysqlclient.so – if not – create it and run ldconfig -v
Run:
1 |
$ mysql -h127.0.0.1 -P9306 |
And then execute SphinxQL:
1 |
mysql> select * from sphinx_test1 where match('four'); |
Expected result:
1 2 3 4 5 6 |
+------+----------+------------+ | id | group_id | date_added | +------+----------+------------+ | 4 | 2 | 1577387479 | +------+----------+------------+ 1 row in set (0.00 sec) |
This shows that you have Sphinx search engine up an running!