Dhtcrawler2 initially uses the mongodb full-text search engine to search for resources. When searching for some short keywords, it is easy to cause the erlang process calltimeout, that is, the query time is too long. For keywords such as avi, the search time is as long as dozens of seconds. The number of searched resources is about 2 million. Most of these resources only index the root file name.
Dhtcrawler2 initially uses the mongodb full-text search engine to search for resources. When searching for some short keywords, it is easy to cause the erlang process call timeout, that is, the query time is too long. For keywords such as avi, the search time is as long as dozens of seconds. The number of searched resources is about 2 million. Most of these resources only index the root file name.
Dhtcrawler2 initially uses the mongodb full-text search engine to search for resources. When searching for some short keywords, it is easy to cause the erlang process call timeout, that is, the query time is too long. For imagesaviThis keyword can be searched for as long as dozens of seconds. The number of searched resources is about 2 million. Most of these resources only index the root file name, that is, for multi-file resources, there is no index for a single file name. Some indexing resources are in the form of string substrings without word splitting, which occupies a lot of storage space; some of them use rmmseg (I compiled rmmseg-cpp and called erl-rmmseg as the erlang nif Library) to split words, which takes a lot less space, however, because of the dictionary problem, many words are not split out.
I thought it was a long time-consuming search because the database was too busy and I wanted to deploy a mongodb cluster. Later, it was found that the database was not read or written, and the query was still slow. Finally, I had to discard the text search provided by mongodb. So I switched to sphinx. For simplicity, I directly downloaded coreseek4.1 (a Chinese Word splitting Package supported by sphsf ).
Now, more than 2 million of resources have been imported into sphbatch, and all file names have been indexed, with the index file reaching 800 mb. ForaviKeyword Search takes about 0.2 seconds. Try searching.
The following records the application of sphinx in dhtcrawler:
Introduction to sphinx
Sphinx contains two main programs: indexer and searchd. Indexer is used to create an index for text content. Then, searchd provides a text search function based on these indexes. To use this function, you can connect to the searchd service using the network protocol of searchd.
Indexer can obtain the text content in multiple ways. The source of the text content is called the data source. Sphinx has a built-in mysql Data source, which means that data can be obtained directly from the mysql database. Sphinx also supports data sources such as xmlpipe2, whose data is provided to indexer in xml format. To import the content in the mongodb database, you can use xmlpipe2.
Sphsf-document
The xmlpipe2 data source must be submitted in the following format:
this is the subject file content 111
This file contains two parts:schemaAnddocuments, WhereschemaThere are two parts:fieldAndattr, IncludingfieldThe identified fields are read by indexer and indexed as all input texts.attrIdentifies the information to be included in the query results;documentsIs composedsphinx:documentThe data that the indexer really wants to process. Note thatschemaThe name of the referenced property.
A very important attribute of document is its id. This id corresponds to the sphtoken that must be unique. The query result also contains this id. Generally, this id can be a database primary key and can be used to query details. The searchd search keyword can be viewed as the search results for these documents. The search results mainly contain the attr specified in the schema.
Incremental Index
Data in the data source is generally changed. The newly added data must be added to the sphsf-index file so that searchd can search for the newly entered data. To continuously add new data, you can use the incremental index mechanism. In the incremental index mechanism, a primary index and a secondary index are required ). Each new data is created as a secondary index, and then merged into the primary index after a period of time. This process mainly uses the indexer and searchd programs. In fact, searchd is a service that requires continuous operation, while indexer is a tool program that exits after the index is created. Therefore, the incremental index mechanism here involves the job of "merge at regular intervals", which requires you to write your own programs for Coordination (or use other tools)
Sphinx and mongodb
As mentioned above, the id of the sphsf-document is generally the primary key of the database used for convenient query. However, by default, a number is not used as the primary key. The resource database of dhtcrawler uses the resource info-hash as the primary key, which cannot be the id of sphsf-document. One solution is to split the hash by bit into several integer numbers supported by sphsf-document attr. For example, info-hash is a 160-bit id. If a 32-bit attr (a higher version of sphinx supports 64-bit integers ), the info-hash can be split into five attr values by bit. The sphsf-document id can use any number, as long as there is no conflict. When the query result is obtained, the corresponding attr is obtained and combined into info-hash.
The default Object id of mongodb can also be split in this way.
Dhtcrawler2 and sphinx
In dhtcrawler2, I wrote an import program myself. This program reads data from mongodb. When the data reaches a certain amount, It outputs an xml file in xmlpipe2 format, creates it as a secondary index, and then merges it into the primary index. The process is very simple, including two external processes, which can be completed through OS: cmd in erlang.
It is worth noting that when reading data from mongodb, using skip is basically unreliable. It takes several minutes for skip to read 1 million pieces of data. To avoid adding additional index fields, I havecreated_atAdd an index to the field, and then read resources by time period. All this is to allow the program to continue the previous job after the restart is disabled, rather than re-starting. 2 million of the data has been processed for several days.
After the data is created, it must be displayed on the front-end. In erlang, there seems to be only one sphinx client library: giza. This library is a little old and seems to be still using sphinx0.9. The query Code contains the version determination and cannot be used in my sphinx2.x version. In desperation, I had to modify the source code of this library. Fortunately, the query function is normal, which means that the communication protocol has not been changed for several versions of sphinx? Later, I modified giza's source code again to obtain the statistics, such as time consumption and summary results. The new version can be found on my github: my giza. Does it seem that I have not violated the version agreement?
Currently, dhtcrawler searches for the hash list based on sphinx, and then searches for the corresponding hash resources in mongodb. In fact, you can directly append the description information of these resources to the document of sphsf-to avoid database queries. However, I think this will increase the size of the sphsf-index file and may affect the search speed. In actual tests, it is found that database queries sometimes consume a lot of time, even though I do paging, so that a single page only performs a small number of queries on the database.
Xml unicode
In the process of importing xml to the sphinx index, the content I output is unicode, but there are many resources that will cause indexer to parse xml errors. After an error occurs, indexer stops processing the current xml directly. Later, I found that the xml content that cannot be processed by indexer contains control characters in unicode, such? (U + 00E4 ). My solution is to directly filter out these control characters. For control characters of unicode, see UTF-8 encoding table and Unicode characters. Doing this in erlang is not complicated:
strip_invalid_unicode(<<>>) -> <<>>;strip_invalid_unicode(<>) -> case is_valid_unicode(C) of true -> RR = strip_invalid_unicode(R), <>; false -> strip_invalid_unicode(R) end;strip_invalid_unicode(<>) -> strip_invalid_unicode(R).is_valid_unicode(C) when C < 16#20 -> false;is_valid_unicode(C) when C >= 16#7f, C =< 16#ff -> false;is_valid_unicode(_) -> true.
Address: http://codemacro.com/2013/08/08/sphinx-dhtcrawler/
Written by Kevin Lynx posted at http://codemacro.com