This page describes the creation of faceted search queries.
Faceted search definition
Faceted search within GroupDocs.Search is a filtering of search results by setting valid document field names to search.
Creating faceted search queries
Faceted search allows you to search only in certain fields of documents, for example, only in the content field or in the file name field. A simple faceted search example is presented below with queries in text and object form.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.optionsimportCommonFieldNames# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search in the content field with text queryresult1=index.search("content: Einstein")# Search in the content field with object queryword_query=SearchQuery.create_word_query("Einstein")field_query=SearchQuery.create_field_query(CommonFieldNames.CONTENT,word_query)result2=index.search(field_query)# Search in all fields, for comparisonresult3=index.search("Einstein")# Printing the documents found and the fields in which the word was foundprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path),[field.field_nameforfieldindocument.found_fields])print("Documents found in all fields:",result3.document_count)fordocumentinresult3:print(" ",os.path.basename(document.document_info.file_path),[field.field_nameforfieldindocument.found_fields])
Documents found by the text query: 2
Documents found by the object query: 2
einstein-biography.txt ['content']
nobel-prize.txt ['content']
Documents found in all fields: 3
albert-einstein-letters.txt ['filename']
einstein-biography.txt ['filename', 'content']
nobel-prize.txt ['content']
Faceted search can be combined with other types of searches using parentheses. The following faceted search example demonstrates the same multi-faceted search query in text and object form. Both queries search for documents whose file name contains both the words “Albert” and “Einstein”, or whose content contains the phrase “theory of relativity” or the phrase “special relativity”.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.optionsimportCommonFieldNames# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search with text queryresult1=index.search("(filename: (Albert AND Einstein)) OR (content: (\"theory of relativity\" OR \"special relativity\"))")# Search with object queryalbert_query=SearchQuery.create_word_query("Albert")einstein_query=SearchQuery.create_word_query("Einstein")and_query=SearchQuery.create_and_query(albert_query,einstein_query)filename_query=SearchQuery.create_field_query(CommonFieldNames.FILE_NAME,and_query)theory_query=SearchQuery.create_word_query("theory")of_query=SearchQuery.create_word_query("of")relativity1_query=SearchQuery.create_word_query("relativity")special_query=SearchQuery.create_word_query("special")relativity2_query=SearchQuery.create_word_query("relativity")phrase1_query=SearchQuery.create_phrase_search_query(theory_query,of_query,relativity1_query)phrase2_query=SearchQuery.create_phrase_search_query(special_query,relativity2_query)or_query=SearchQuery.create_or_query(phrase1_query,phrase2_query)content_query=SearchQuery.create_field_query(CommonFieldNames.CONTENT,or_query)root_query=SearchQuery.create_or_query(filename_query,content_query)result2=index.search(root_query)# Printing the documents found and the fields in which they were foundprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path),[field.field_nameforfieldindocument.found_fields])
Documents found by the text query: 3
Documents found by the object query: 3
albert-einstein-letters.txt ['filename']
einstein-biography.txt ['content']
relativity-primer.txt ['content']
There are also fields that may be present in documents of any type. The names of such fields are represented in the CommonFieldNames class.
An example of using standard field names of documents is presented in the following example.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.optionsimportWordsFieldNames# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Search in the company field with text queryquery1=WordsFieldNames.COMPANY+": Dycum"result1=index.search(query1)# Search in the company field with object queryword_query=SearchQuery.create_word_query("Dycum")field_query=SearchQuery.create_field_query(WordsFieldNames.COMPANY,word_query)result2=index.search(field_query)# Printing the documents found and the fields in which the word was foundprint("Documents found by the text query:",result1.document_count)print("Documents found by the object query:",result2.document_count)fordocumentinresult2:print(" ",os.path.basename(document.document_info.file_path),[field.field_nameforfieldindocument.found_fields])