searchengine/cpix/cpix/src/prefixopt.cpp
changeset 1 6f2c1c46032b
child 8 6547bf8ca13a
equal deleted inserted replaced
0:671dee74050a 1:6f2c1c46032b
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "prefixopt.h"
       
    19  
       
    20 #include "CLucene.h"
       
    21 
       
    22 #include "cpixsearch.h"
       
    23 
       
    24 #include "cpixstrtools.h"
       
    25 
       
    26 namespace Cpix {
       
    27 
       
    28 	using namespace lucene::search;
       
    29 
       
    30 	using namespace lucene::index;
       
    31 	
       
    32 	PrefixOptQueryRewriter::PrefixOptQueryRewriter(
       
    33 			int maxPrefixLength, 
       
    34 			const wchar_t* optimizedField, 
       
    35 			const wchar_t* prefixField )
       
    36 	: 	maxPrefixLength_( maxPrefixLength ),
       
    37 		optimizedField_( optimizedField ),
       
    38 		prefixField_( prefixField )
       
    39 	{}
       
    40 
       
    41 
       
    42 	PrefixOptQueryRewriter::~PrefixOptQueryRewriter()
       
    43 	{}
       
    44 	
       
    45 
       
    46 	std::auto_ptr<Query> PrefixOptQueryRewriter::rewrite(auto_ptr<Query> query)
       
    47 	{
       
    48 		PrefixQuery* wildq = 
       
    49 			dynamic_cast<PrefixQuery*>( query.get() );
       
    50 
       
    51 		if ( wildq )
       
    52 		{
       
    53 			Term* term = wildq->getPrefix(false);
       
    54 			
       
    55 			if ( optimizedField_ == term->field() )
       
    56 			{
       
    57 				int length = wcslen( term->text() );
       
    58 				if ( length != -1 && length <= maxPrefixLength_ )
       
    59 				{
       
    60 					// rewrite term
       
    61 					std::wstring text( term->text() ); 
       
    62 					text = text.substr(0, length); 
       
    63 					term = new Term(prefixField_.c_str(), text.c_str(), true);
       
    64 				
       
    65 					// rewrite query
       
    66 					std::auto_ptr<Query> ret( new TermQuery( term ) );
       
    67 					query.reset(); // delete old
       
    68 					return ret; // return ownership 
       
    69 				}
       
    70 			}
       
    71 		}
       
    72 		BooleanQuery* boolq = 
       
    73 			dynamic_cast<BooleanQuery*>( query.get() ); 
       
    74 		
       
    75 		if ( boolq ) 
       
    76 		{
       
    77 			// Just modify the query
       
    78 			Cpt::auto_array<BooleanClause*> clauses( boolq->getClauses() );
       
    79 			
       
    80 			for ( int i = 0; i < boolq->getClauseCount(); i++ ) 
       
    81 			{
       
    82 				// Transfer ownership to stack
       
    83 				std::auto_ptr<Query> clauseq(
       
    84 					clauses.get()[i]->query );
       
    85 				clauses.get()[i]->query = 0;
       
    86 				
       
    87 				// Rewrite and restore ownership
       
    88 				clauseq = rewrite( clauseq );
       
    89 				clauses.get()[i]->query = clauseq.release(); 
       
    90 			}
       
    91 		}
       
    92 		
       
    93 		return query; // return ownership 
       
    94 	}
       
    95 	
       
    96 }