A method and apparatus for searching text information
By adjusting the Ngram segmentation boundary values of the ES index and establishing matching documents, the problem of redundant terms caused by excessively large segmentation boundary values was solved, achieving more efficient text search.
Patent Information
- Application Number
- CN202211619771.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-15
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2042-12-15
AI Technical Summary
In existing technologies, Ngram segmentation sets excessively large segmentation boundary values, resulting in a large amount of redundancy when text blocks are converted into distinct terms, wasting storage resources and slowing down search performance.
By adjusting the Ngram segmentation boundary values in the Elasticsearch index, matching documents are built using the information to be searched, and segmentation and text matching are performed according to the adjusted boundary values, reducing redundant terms and improving search efficiency.
This reduces the number of terms after word segmentation, saves storage resources, shortens search time, and improves search efficiency.
Smart Images

Figure CN115827825B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of text search, and more particularly to a method and apparatus for searching text information. Background Technology
[0002] With the development of natural language processing technology, searching for relevant information based on words in different application scenarios has gradually relied on artificial intelligence algorithms. In particular, users usually search for information by entering text content such as words. At this time, the execution end will use algorithm models to search for words and other text content to find matching information for the user.
[0003] One common search method is to use an Elasticsearch index analyzer equipped with Ngrams (N-gram parsers) for search analysis. Specifically, it converts text blocks into distinct and normalized terms. The mapping relationship between the analyzed term list and documents is stored in an inverted index table. Each query quickly matches the document for each term in the inverted index table, and then the documents are deduplicated, sorted, merged, and returned to the page for display.
[0004] However, the commonly used methods have the following technical problems: the segmentation boundary value set by Ngram segmentation is too large, which converts text blocks into distinct terms and generates a large number of redundant and useless terms, resulting in a large amount of term storage and wasting storage resources; moreover, due to the large segmentation boundary value, each search consumes a lot of time for segmentation, which slows down search performance and reduces search efficiency. Summary of the Invention
[0005] This invention proposes a general text search method and apparatus. After obtaining the search information input by the user, the method adjusts the Ngram segmentation boundary values in the Elasticsearch (ES) index based on the phrases in the search information. Simultaneously, it uses the search information to build matching documents that can be connected to the ES index. During the search, the information is segmented according to the Ngram segmentation boundary values, and then the segmented words are matched with the matching documents to filter the search information that matches the user. This not only reduces the number of terms after segmentation, avoiding the generation of a large number of redundant and useless terms, but also shortens the search time and improves search efficiency.
[0006] A first aspect of this invention provides a method for searching text information, the method comprising:
[0007] Obtain the search information input by the user, adjust the Ngram segmentation boundary value in the ES index based on the word groups contained in the search information, and use the search information to build a matching document connected to the ES index;
[0008] The search text information is segmented according to the Ngram segmentation boundary values to obtain several segmented terms;
[0009] The target search text is obtained by performing text matching between the segmented terms and the matching document.
[0010] In one possible implementation of the first aspect, adjusting the Ngram segmentation boundary values in the ES index based on the phrases contained in the search information includes:
[0011] Count the number of phrases contained in the information to be searched;
[0012] The search information is divided into several name phrases according to the number of phrases, and the word length of each name phrase is determined.
[0013] Adjust the Ngram segmentation boundary values in the ES index according to the stated word length values.
[0014] In one possible implementation of the first aspect, the step of constructing a matching document connected to the ES index using the search information includes:
[0015] The search information is added to a preset blank document to obtain a matching document;
[0016] Call the ES index to add a new document interface, and insert the matching document into the document interface.
[0017] In one possible implementation of the first aspect, the step of performing text matching between the segmented terms and the matching document to obtain the target search text includes:
[0018] According to the matching document, phrase matching is performed on several segmented word items to obtain several matching phrases;
[0019] The target search text is obtained by sorting and deduplicating several of the matching phrases.
[0020] A second aspect of the present invention provides a text information search device, the device comprising:
[0021] The adjustment and creation module is used to obtain the search information input by the user, adjust the Ngram segmentation boundary value in the ES index based on the word groups contained in the search information, and use the search information to create a matching document connected to the ES index.
[0022] The word segmentation module is used to segment the text information to be searched according to the Ngram word segmentation boundary value to obtain a number of word segments;
[0023] The search module is used to perform text matching between the segmented terms and the matching documents to obtain the target search text.
[0024] In one possible implementation of the second aspect, the adjustment and establishment module is further configured to:
[0025] Count the number of phrases contained in the information to be searched;
[0026] The search information is divided into several name phrases according to the number of phrases, and the word length of each name phrase is determined.
[0027] Adjust the Ngram segmentation boundary values in the ES index according to the stated word length values.
[0028] In one possible implementation of the second aspect, the adjustment and establishment module is further configured to:
[0029] The search information is added to a preset blank document to obtain a matching document;
[0030] Call the ES index new document interface and insert the matching document into the document interface.
[0031] In one possible implementation of the second aspect, the search module is further configured to:
[0032] According to the matching document, phrase matching is performed on several segmented word items to obtain several matching phrases;
[0033] The target search text is obtained by sorting and deduplicating several of the matching phrases.
[0034] Compared with existing technologies, the text information search method and apparatus provided by this invention have the following advantages: After obtaining the search information input by the user, this invention adjusts the Ngram segmentation boundary values in the ES index based on the phrases in the search information. At the same time, it uses the search information to build a matching document that can be connected to the ES index. During the search, the information is segmented according to the Ngram segmentation boundary values, and then the segmented words are matched with the matching document to filter the search information that matches the user. By segmenting words using the adjusted Ngram segmentation boundary values, not only is the number of terms after segmentation reduced, avoiding the generation of a large number of redundant and useless terms, but the search time can also be shortened, and the search efficiency can be improved. Attached Figure Description
[0035] Figure 1 This is a flowchart illustrating a text information search method according to an embodiment of the present invention;
[0036] Figure 2This is a flowchart of the word-skipping matching operation provided in an embodiment of the present invention;
[0037] Figure 3 This is a flowchart illustrating the operation of a text information search method according to an embodiment of the present invention;
[0038] Figure 4 This is a schematic diagram of the structure of a text information search device provided in an embodiment of the present invention. Detailed Implementation
[0039] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0040] With the development of natural language processing technology, searching for relevant information based on words in different application scenarios has gradually relied on artificial intelligence algorithms. In particular, users usually search for information by entering text content such as words. At this time, the execution end will use algorithm models to search for words and other text content to find matching information for the user.
[0041] One common search method is to use an Elasticsearch index analyzer equipped with Ngrams (N-gram parsers) for search analysis. Specifically, it converts text blocks into distinct and normalized terms. The mapping relationship between the analyzed term list and documents is stored in an inverted index table. Each query quickly matches the document for each term in the inverted index table, and then the documents are deduplicated, sorted, merged, and returned to the page for display.
[0042] However, the commonly used methods have the following technical problems: the segmentation boundary value set by Ngram segmentation is too large, which converts text blocks into distinct terms and generates a large number of redundant and useless terms, resulting in a large amount of term storage and wasting storage resources; moreover, due to the large segmentation boundary value, each search consumes a lot of time for segmentation, which slows down search performance and reduces search efficiency.
[0043] To address the aforementioned issues, the following specific embodiments will provide a detailed description and explanation of a text information search method provided in this application.
[0044] Reference Figure 1 The diagram shows a flowchart of a text information search method provided by an embodiment of the present invention.
[0045] By way of example, the method for searching text information may include:
[0046] S11: Obtain information to be searched input by a user, adjust an Ngram tokenization boundary value in an ES index based on word groups included in the information to be searched, and use the information to be searched to create a matching document connected to the ES index.
[0047] The information to be searched may be information of a text that the user wants to search for, such as a product name, an enterprise name, a personnel number, and the like. For example, "xx curtain fabric", "one-yuan commodity", "xx candy", etc.
[0048] After the information to be searched of the user is determined, word groups included in the information to be searched may be determined first, and then the Ngram tokenization boundary value in the ES index is adjusted based on each word group, so as to adjust a boundary value during search tokenization, thereby avoiding a situation in which an excessively large tokenization boundary value results in an excessively large number of tokenized terms, long tokenization time, and a large number of stored terms.
[0049] Then, a matching document may be created by using the content of the information to be searched, and a connection is established between the matching document and the ES index, so that information matching can be performed in a subsequent step according to the matching document, to quickly screen and find the text content required by the user.
[0050] By way of example, step S11 may include the following sub-steps:
[0051] S111: Count the number of word groups included in the information to be searched.
[0052] S112: Split the information to be searched into several name word groups according to the value of the number of word groups, and determine a word length value of each name word group.
[0053] S113: Adjust the Ngram tokenization boundary value in the ES index according to the word length value.
[0054] For example, the information to be searched is "one-yuan commodity", word groups included therein are "one", "yuan", "commodity", and "product", and the counted number of word groups is 4.
[0055] Then, the information is split according to 4 to obtain four word groups: "one", "yuan", "commodity", and "product", and a word length value of each word group is obtained, where the word length value is 1.
[0056] Finally, 1 is used as a tokenization boundary value to adjust the Ngram tokenization boundary value in the ES index.
[0057] Specifically, the word length value can be used to set the parameters of multi_match: min_gram=1, max_gram=1. min_gram specifies the minimum length of the word group after word segmentation, and max_gram specifies the maximum length of the word group after word segmentation, which can be understood as 1<=length of each word group after word segmentation<=1.
[0058] ES configures the index analyzer analysis in setting. Assuming that the tokenizer named ngram_analyzer sets the key parameters min_gram and max_gram to 1, it can reduce redundant storage and improve search speed.
[0059] For further explanation, assuming that the original Ngram word segmentation boundary value is 64, and its parameters are min_gram=1 and max_gram=64, while the parameters of the present invention are min_gram=1 and max_gram=1.
[0060] The following are all word groups obtained by performing word segmentation on "I am Chinese" according to the two word segmentation rules:
[0061] Word segmentation results of the original parameters: "I", "I am", "I am a", "I am a Chinese", "I am Chinese", "am", "am a", "am a Chinese", "am Chinese", "a", "a Chinese", "Chinese", "China", "Chinese people", "people".
[0062] Optimized word segmentation results: "I", "am", "a", "Chinese", "people".
[0063] It can be seen from the above example that if the Ngram word segmentation boundary value is too large, no words will be removed during word segmentation, which will result in a very large number of word items after word segmentation and generate a large number of useless word items. After adjusting the Ngram word segmentation boundary value, the number of word items after word segmentation can be greatly reduced, avoiding generating a large number of useless word items.
[0064] After adjustment, min_gram specifies the minimum length of the word group after word segmentation, and max_gram specifies the maximum length of the word group after word segmentation.
[0065] For example, if min_gram=3 and max_gram=4, it means 3<=length of each word group after word segmentation<=4. The word segmentation result of "I am Chinese" is: "I am a", "I am a Chinese", "am a Chinese", "am Chinese", "a Chinese", and there will be no word group with a length exceeding 3 Chinese characters.
[0066] It should be noted that the Ngram segmentation boundary values can also be adjusted according to actual needs. For example, they can be adjusted based on the minimum word length value among multiple word length values, or they can be adjusted based on the maximum word length value among multiple word length values.
[0067] The optimized Ngram segmentation reduces the number of terms by more than 6 times, greatly saving hard drive space and improving search speed.
[0068] After adjusting the Ngram segmentation boundary values, you can set the matching document based on the search information entered by the user, which facilitates subsequent text matching.
[0069] As an example, step S11 may also include the following sub-steps:
[0070] S114. Add the information to be searched to a preset blank document to obtain a matching document.
[0071] S115. Call the ES index new document interface and insert the matching document into the document interface.
[0072] In one embodiment, matching documents can be obtained by adding the information to be searched to a blank document, and then the ES indexing add document interface can be called to insert the matching document into the document interface.
[0073] Continuing with the example above, we can call the ES index add document interface to insert a document with the value "one yuan product" for subsequent test searches.
[0074] It should be noted that users can set different search languages as needed, and set text in that language as the corresponding matching document. This allows users to search in various languages, and quickly find the entire text using any single word that makes up the text.
[0075] In addition, this invention also supports unordered search and word-skipping search. For example, searching for "I am Chinese" or "China I" will return results.
[0076] S12. The text information to be searched is segmented according to the Ngram segmentation boundary value to obtain several segmented terms.
[0077] The text information to be searched is the database information to be searched.
[0078] Specifically, tokenization may be performed according to Ngram tokenization boundary values (corresponding multi_match parameters: min_gram=1, max_gram=1, where min_gram specifies the minimum length of tokenized phrases, and max_gram specifies the maximum length of tokenized phrases, understood as 1<=length of each tokenized phrase<=1).
[0079] Assuming that a paragraph of text information to be searched contains 5 Chinese characters, it may be divided into 5 groups of words, each group of words having a length of 1.
[0080] Further assuming that a paragraph of text information to be searched contains 10 Chinese characters, it may be divided into 10 groups of words, each group of words having a length of 1.
[0081] S13: Performing text matching between the tokenized terms and the matching document to obtain a target search text.
[0082] Since the tokenized terms may include multiple tokenized terms or contents irrelevant to the input information to be searched, several tokenized terms may be filtered by text matching according to the content of the matching document to obtain relevant tokenized terms; then the relevant tokenized terms are assembled to obtain the target search text required by the user.
[0083] In one embodiment, step S13 may include the following sub-steps:
[0084] S131: Performing phrase matching on several of the tokenized terms according to the matching document to obtain several matching phrases.
[0085] S132: Deduplicating and sorting several of the matching phrases to obtain a target search text.
[0086] For example, assuming that a paragraph of text information to be searched contains 10 Chinese characters, it may be divided into 10 groups of words, each group of words having a length of 1. If the information to be searched required by the user is "一元商品" (one-yuan commodity), and "一元商品" is also made into a matching document, then 4 tokenized terms of "一", "元", "商", and "品" may be filtered from the 10 groups of words according to the matching document, and then these 4 tokenized terms are deduplicated and sorted according to the matching document to obtain the information "一元商品".
[0087] In specific implementation, it may be implemented through search code.
[0088] Taking the JAVA language implementation as an example, a multi_match query is used, with the parameter slop set to "30" and operator set to "AND", to implement phrase combination matching and skipped-word matching.
[0089] The slop parameter specifies how far apart terms in a multi_match query can be while still considering a document as a match. Setting the operator to "AND" means that all search terms must be present in the same field of the index for a document to be matched.
[0090] For example: the Ngram inverted index table stores term lists such as "一", "元", "商", "品" and their corresponding mapping relationships to the document "一元商品". The search keyword "一品" does not exist in the inverted index table, but the individual terms "一" and "品" do exist, and "一" and "品" are in the same field.
[0091] Phrase combination matching will respectively match the documents corresponding to "一" and "品", i.e., the document "一元商品", and then deduplicate and sort the searched document data.
[0092] Then the document is matched by the keyword and returned. Specifically, the document that meets the keyword can be searched, and "一元商品" is returned to the page for display.
[0093] Referring to Figure 2 , a flowchart of the skip-word matching operation provided by an embodiment of the present invention is shown.
[0094] In one embodiment, the phrase matching may be skip-word matching. Skip-word matching requires all words to appear in order, but by setting the slop parameter of multi_match, words can be arranged in any order.
[0095] Combined with Figure 2 the position corresponding to each term to understand the slop principle: the search phrase is "我国", and slop=10 is set. Since "我" directly finds the term at position 0, no skip is needed. "国" is not found at position 1, so "国" skips one word to position 2, which corresponds to "中", and the term is not found.
[0096] "国" skips two words to position 3, which corresponds to "国", and the term is found to stop skipping. At this time, both terms "我国" are found, and the document "我是中国人" meets the search result and is returned to the page for display.
[0097] If the slop length is greater than the number of terms, for example, "国" skips four words, it will return to the position of position 0. That is to say, if the skip word reaches the tail of the term, skipping another word will return to the head of the term, and then the skip word matching will be performed cyclically from the head of the term to the tail of the term until the term is matched.
[0098] If no term is matched in the complete process from the term head to the term tail, the process will also end, indicating that no matching term is found and the document does not meet the search criteria.
[0099] When performing deduplication and sorting to match each phrase combination, you can set the multi_match parameter operator=AND, so that two search phrases separated by spaces can both match the document using phrase matching. If the two search phrases are in the same document, the entire search keyword is considered to match the document.
[0100] For example, when searching for "我人", "我" and "人" are respectively matched in "我是中国人", so "我人" is considered to match the document "我是中国人".
[0101] Refer to Figure 3 , which illustrates an operation flowchart of a method for searching text information provided by an embodiment of the present invention.
[0102] In a specific implementation, the specific operation process of the method for searching text information includes the following steps:
[0103] Step 1: ES index configuration.
[0104] ES configures the index analyzer analysis in the setting. Assume the tokenizer is named ngram_analyzer. The key parameters min_gram and max_gram of ngram_analyzer are both set to 1, which mainly serves to reduce redundant storage and improve search speed.
[0105] Step 2: Add a new document.
[0106] Call the ES index add document interface to insert a document with the value "一元商品" for subsequent test searches.
[0107] Step 3: The storage process of data tokenized by Ngram.
[0108] Use ngram_analyzer to divide the document value "一元商品" (Q: Please explain what the document value refers to, whether it refers to the document in the above S2) into multiple term lists. The relationship between the term lists and the document is saved in the inverted index table, and the document value is stored in the index field.
[0109] Step 4: Enter search keywords on the page.
[0110] The possible search keywords entered by the user are listed, and the search in languages of other countries in the world follows the same logic.
[0111] Step 5: Implementation of search code.
[0112] Taking the JAVA language implementation as an example, use the multi_match query, set the parameter slop to "30", and operator to "AND" to achieve phrase combination matching and skip-word matching.
[0113] The slop parameter tells the multi_match query how far apart the terms can be while still matching a document. Setting operator to "AND" means that all search terms must be in the same field of the index to match a document.
[0114] For example, the Ngram inverted index table stores the term lists of "一", "元", "商", "品" and their corresponding mapping relationships with the document "一元商品". The search keyword "一品" does not exist in the inverted index table, but the individual terms "一" and "品" do exist, and "一" and "品" are in the same field.
[0115] Phrase combination matching will match the document "一元商品" corresponding to "一" and "品" respectively, and then deduplicate and sort the searched document data.
[0116] Step 6: The keyword matches the document and returns the result.
[0117] Search for documents that match the keyword and return "一元商品" to the page for display.
[0118] The present invention has simple configuration. Developers do not need to install additional ES word segmentation plugins, and only use the built-in Ngram analyzer to meet the search needs of all foreign languages. One word segmentation configuration solves the trouble of global language word segmentation compatibility issues. At the same time, the number of word segmentations is extremely small, which not only meets business needs but also saves hard disk space, and can greatly improve search efficiency.
[0119] In this embodiment, the embodiment of the present invention provides a method for searching text information. Its beneficial effect is that the present invention can, after acquiring the to-be-searched information input by the user, adjust the Ngram word segmentation boundary value in the ES index according to the word group of the to-be-searched information, and simultaneously use the to-be-searched information to establish a matching document that can be connected to the ES index. When searching, the information is segmented according to the Ngram word segmentation boundary value, and then the word segmentation is matched with the matching document to filter the to-be-searched information that meets the user's requirements. Performing word segmentation through the adjusted Ngram word segmentation boundary value not only reduces the number of terms after word segmentation, avoids generating a large number of redundant and useless terms, but also shortens the search time and improves search efficiency.
[0120] The embodiment of the present invention further provides a device for searching text information, see Figure 4 , which illustrates a schematic structural diagram of a device for searching text information provided by an embodiment of the present invention.
[0121] As an example, the text information search device may include:
[0122] The adjustment and creation module 401 is used to obtain the search information input by the user, adjust the Ngram segmentation boundary value in the ES index based on the word groups contained in the search information, and use the search information to create a matching document connected to the ES index.
[0123] The word segmentation module 402 is used to segment the text information to be searched according to the Ngram word segmentation boundary value to obtain a number of word segments;
[0124] The search module 403 is used to perform text matching between the segmented terms and the matching document to obtain the target search text.
[0125] Optionally, the adjustment and establishment module is further configured to:
[0126] Count the number of phrases contained in the information to be searched;
[0127] The search information is divided into several name phrases according to the number of phrases, and the word length of each name phrase is determined.
[0128] Adjust the Ngram segmentation boundary values in the ES index according to the stated word length values.
[0129] Optionally, the adjustment and establishment module is further configured to:
[0130] The search information is added to a preset blank document to obtain a matching document;
[0131] Call the ES index new document interface and insert the matching document into the document interface.
[0132] Optionally, the search module is further configured to:
[0133] According to the matching document, phrase matching is performed on several segmented word items to obtain several matching phrases;
[0134] The target search text is obtained by sorting and deduplicating several of the matching phrases.
[0135] Those skilled in the art will understand that, for ease of description and brevity, the specific working process of the device described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0136] Furthermore, this application also provides an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the text information search method as described in the above embodiments.
[0137] Furthermore, embodiments of this application also provide a computer-readable storage medium storing a computer-executable program for causing a computer to perform the text information search method as described in the above embodiments.
[0138] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.
Claims
1. A method for searching text information, characterized in that, The method includes: Obtain the search information input by the user, adjust the Ngram segmentation boundary value in the ES index based on the word groups contained in the search information, and use the search information to build a matching document connected to the ES index; The search text information is segmented according to the Ngram segmentation boundary values to obtain several segmented terms; The target search text is obtained by performing text matching between the segmented terms and the matching document. The adjustment of the Ngram segmentation boundary values in the ES index based on the phrases contained in the search information includes: Count the number of phrases contained in the information to be searched; The search information is divided into several name phrases according to the number of phrases, and the word length of each name phrase is determined. Adjust the Ngram segmentation boundary values in the ES index according to the stated word length values.
2. The text information search method according to claim 1, characterized in that, The process of creating matching documents that are linked to the ES index using the information to be searched includes: The search information is added to a preset blank document to obtain a matching document; Call the ES index new document interface and insert the matching document into the document interface.
3. The text information search method according to claim 1, characterized in that, The step of performing text matching between the segmented terms and the matching document to obtain the target search text includes: According to the matching document, phrase matching is performed on several segmented word items to obtain several matching phrases; The target search text is obtained by sorting and deduplicating several of the matching phrases.
4. A text information search device, characterized in that, The device includes: The adjustment and creation module is used to obtain the search information input by the user, adjust the Ngram segmentation boundary value in the ES index based on the word groups contained in the search information, and use the search information to create a matching document connected to the ES index. The word segmentation module is used to segment the text information to be searched according to the Ngram word segmentation boundary value to obtain a number of word segments; The search module is used to perform text matching between the segmented terms and the matching document to obtain the target search text; The adjustment and establishment module is also used for: Count the number of phrases contained in the information to be searched; The search information is divided into several name phrases according to the number of phrases, and the word length of each name phrase is determined. Adjust the Ngram segmentation boundary values in the ES index according to the stated word length values.
5. The text information search device according to claim 4, characterized in that, The adjustment and establishment module is also used for: The search information is added to a preset blank document to obtain a matching document; Call the ES index new document interface and insert the matching document into the document interface.
6. The text information search device according to claim 4, characterized in that, The search module is also used for: According to the matching document, phrase matching is performed on several segmented word items to obtain several matching phrases; The target search text is obtained by sorting and deduplicating several of the matching phrases.
7. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, when the processor executes the program, it implements the text information search method as described in any one of claims 1-3.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer-executable program, which is used to cause a computer to perform the text information search method as described in any one of claims 1-3.
Citation Information
Patent Citations
Content query method and device
CN113326346A
Report retrieval method and device
CN115145913A
