concept analysis in category search

appears as: nalysis, Analysis, alysis
Relevant Search: With applications for Solr and Elasticsearch

This is an excerpt from Manning's book Relevant Search: With applications for Solr and Elasticsearch.

After being handed to the search engine, the fields of a document undergo a process called analysis, shown in figure 2.3.

Figure 2.3. “Barcelona Beaches” article indexed and analyzed (only title-field analysis shown)

Analysis converts the field values (usually text) into elements called tokens. For text, tokens usually correspond to words, such as “best”, “barcelona”, “beach”. You’ll notice that these tokens look a bit different from the original words. In this case, one word (the) has been dropped, the tokens are lowercased, and the plural suffix of beaches has been removed. Why? A token extracted from our article and a token extracted from a future search query typically need to match exactly to be considered a match. Search often aids in matching by using language-specific heuristics to transform words to simpler forms. English text analysis removes capitalization (RUN -> run), suffixes (running -> run), and pluralization (runs -> run) and many other patterns. And although tokens are typically generated text, as you’ll see in chapter 4, analysis can be applied and tokens generated for nontext values such as floating-point numbers and geographic locations.

In chapter 1, we mentioned the notion of features. In machine learning, features are descriptors for the items being classified. Features used to classify fruit may be things such as color, flavor, and shape. With full-text search, the tokens produced during analysis are the dominant features used to match a user’s query with documents in the index. Don’t worry if this seems vague right now; the greater portion of this book is dedicated to making these ideas clear.

After analysis is complete, the documents are indexed; the tokens from the analysis step are stored into search engine data structures for document retrieval. In addition, the original, untokenized text fields are stored so that they can be presented back to the user in search results. Storing numeric fields also allows numerical attributes to be used in ranking calculations.

This is why analysis is so important. It’s why you spend a great deal of time normalizing text so that query tokens will match documents even if the original document text isn’t quite the same as the query text. For instance, looking at the alternative analysis in listing 2.5, when we normalize the example sentence to remove capitalization, apostrophes, and accents, then the same query for “brown cafe ole” is a perfect match to our example document on all three terms.

The final step is token filtering. Here the stream of tokens can be adjusted, either by adding or removing the tokens or by changing them. In order to appropriately normalize the tokens from our sample sentence, a typical choice would be to lowercase the tokens, remove common words such as the and at (these common words are called stop words), and remove the possessive after Brown. And like the character filter, several token filters can be applied in series during analysis in the order prescribed by the relevance engineer. Token filtering is shown in figure 2.8.

Figure 2.8. Analysis—token filtering

One final note before we move on to indexing. During analysis, it’s common to store extra metadata with each token that the analysis process generates. The most common metadata are the term positions and term offsets, which are useful for phrase queries and highlighting, respectively. You can also create custom token filters that add arbitrary metadata to the tokens in values called payloads. But beware: all this data can consume a lot of storage. If you must tinker around with payloads, it’s wise to be conservative. Please refer to table 2.1 for more details on these items. (We also bring these up again as needed later in this book.)

Lucene in Action, Second Edition

This is an excerpt from Manning's book Lucene in Action, Second Edition.

This is perfectly acceptable and encouraged, as it’s a natural way to represent a field that legitimately has multiple values. Internally, whenever multiple fields with the same name appear in one document, both the inverted index and term vectors will logically append the tokens of the field to one another, in the order the fields were added. You can use advanced options during analysis that control certain important details of this appending, notably how to prevent searches from matching across two different field values; see section 4.7.1 for details. But, unlike indexing, when the fields are stored they’re stored separately in order in the document, so that when you retrieve the document at search time you’ll see multiple Field instances.

Analysis, in Lucene, is the process of converting field text into its most fundamental indexed representation, terms. These terms are used to determine what documents match a query during searching. For example, if you indexed this sentence in a field the terms might start with for and example, and so on, as separate terms in sequence. An analyzer is an encapsulation of the analysis process. An analyzer tokenizes text by performing any number of operations on it, which could include extracting words, discarding punctuation, removing accents from characters, lowercasing (also called normalizing), removing common words, reducing words to a root form (stemming), or changing words into the basic form (lemmatization). This process is also called tokenization, and the chunks of text pulled from a stream of text are called tokens. Tokens, combined with their associated field name, are terms.

Listing 4.1. AnalyzerDemo: seeing analysis in action

4.1.3. Parsing vs. analysis: when an analyzer isn’t appropriate

An important point about analyzers is that they’re used internally for textual fields enabled for analysis. Document formats such as HTML, Microsoft Word, XML, and others contain metadata such as the author, the title, the last modified date, and potentially much more. When you’re indexing rich documents, this metadata should be separated and indexed as separate fields. Analyzers are used to analyze a specific field at a time and break things into tokens only within that field; creating new fields isn’t possible within an analyzer.

Analyzers don’t help in field separation because their scope is to deal with a single field at a time. Instead, parsing these documents prior to analysis is required. For example, it’s a common practice to separate at least the <title> and <body> of HTML documents into separate fields. In these cases, the documents should be parsed, or preprocessed, into separate blocks of text representing each field. Chapter 7 covers this preprocessing step in detail.