summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/queryParser/MultiFieldQueryParser.cpp
blob: dbe0c9472cd5d44eace7419a0fced6bb5739a10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
*
* Changes are Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "MultiFieldQueryParser.h"
#include "CLucene/analysis/AnalysisHeader.h"
#include "CLucene/search/BooleanQuery.h"
#include "CLucene/search/PhraseQuery.h"
#include "CLucene/search/SearchHeader.h"
#include "QueryParser.h"

CL_NS_USE(index)
CL_NS_USE(util)
CL_NS_USE(search)
CL_NS_USE(analysis)

CL_NS_DEF(queryParser)

MultiFieldQueryParser::MultiFieldQueryParser(const TCHAR** fields, CL_NS(analysis)::Analyzer* a, BoostMap* boosts):
	QueryParser(NULL,a)
{
	this->fields = fields;
    this->boosts = boosts;
}
MultiFieldQueryParser::~MultiFieldQueryParser(){
}

//static 
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** fields, Analyzer* analyzer)
{
    BooleanQuery* bQuery = _CLNEW BooleanQuery();
    int32_t i = 0;
    while ( fields[i] != NULL ){
		   Query* q = QueryParser::parse(query, fields[i], analyzer);
			bQuery->add(q, true, false, false);

        i++;
    }
    return bQuery;
}

//static 
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** fields, const uint8_t* flags, Analyzer* analyzer)
{
    BooleanQuery* bQuery = _CLNEW BooleanQuery();
    int32_t i = 0;
    while ( fields[i] != NULL )
    {
		Query* q = QueryParser::parse(query, fields[i], analyzer);
        uint8_t flag = flags[i];
        switch (flag)
        {
			case MultiFieldQueryParser::REQUIRED_FIELD:
                bQuery->add(q, true, true, false);
                break;
            case MultiFieldQueryParser::PROHIBITED_FIELD:
                bQuery->add(q, true, false, true);
                break;
            default:
                bQuery->add(q, true, false, false);
                break;
        }

        i++;
    }
    return bQuery;
}


Query* MultiFieldQueryParser::GetFieldQuery(const TCHAR* field, TCHAR* queryText, int32_t slop){
	if (field == NULL) {
		CL_NS_STD(vector)<BooleanClause*> clauses;
		for (int i = 0; fields[i]!=NULL; ++i) {
			Query* q = QueryParser::GetFieldQuery(fields[i], queryText);
			if (q != NULL) {
                //If the user passes a map of boosts
                 if (boosts != NULL) {
                     //Get the boost from the map and apply them
                     BoostMap::const_iterator itr = boosts->find(fields[i]);
                     if (itr != boosts->end()) {
                         q->setBoost(itr->second);
                     }
                 }
				if (q->getQueryName() == PhraseQuery::getClassName()) {
					((PhraseQuery*)q)->setSlop(slop);
				}
				//if (q instanceof MultiPhraseQuery) {
				//	((MultiPhraseQuery) q).setSlop(slop);
				//}
				q = QueryAddedCallback(fields[i], q);
				if ( q )
					clauses.push_back(_CLNEW BooleanClause(q, true, false,false));
			}
		}
		if (clauses.size() == 0)  // happens for stopwords
			return NULL;
		Query* q = QueryParser::GetBooleanQuery(clauses);
        return q;
	}else{
		Query* q = QueryParser::GetFieldQuery(field, queryText);
		if ( q )
			q = QueryAddedCallback(field,q);
		return q;
	}
}


Query* MultiFieldQueryParser::GetFieldQuery(const TCHAR* field, TCHAR* queryText){
	return GetFieldQuery(field, queryText, 0);
}


CL_NS(search)::Query* MultiFieldQueryParser::GetFuzzyQuery(const TCHAR* field, TCHAR* termStr){
	if (field == NULL) {
		CL_NS_STD(vector)<BooleanClause*> clauses;
		for (int i = 0; fields[i]!=NULL; ++i) {
			Query* q = QueryParser::GetFuzzyQuery(fields[i], termStr); //todo: , minSimilarity
			if ( q ){
				q = QueryAddedCallback(fields[i], q);
				if ( q ){
					clauses.push_back(_CLNEW BooleanClause(q,true,false,false) );
				}
			}
		}
		return QueryParser::GetBooleanQuery(clauses);
	}else{
		Query* q = QueryParser::GetFuzzyQuery(field, termStr);//todo: , minSimilarity
		if ( q )
			q = QueryAddedCallback(field,q);
		return q;
	}
}

Query* MultiFieldQueryParser::GetPrefixQuery(const TCHAR* field, TCHAR* termStr){
	if (field == NULL) {
		CL_NS_STD(vector)<BooleanClause*> clauses;
		for (int i = 0; fields[i]!=NULL; ++i) {
			Query* q = QueryParser::GetPrefixQuery(fields[i], termStr);
			if ( q ){
				q = QueryAddedCallback(fields[i],q);
				if ( q ){
					clauses.push_back(_CLNEW BooleanClause(q,true,false,false));
				}
			}
		}
		return QueryParser::GetBooleanQuery(clauses);
	}else{
		Query* q = QueryParser::GetPrefixQuery(field, termStr);
		if ( q )
			q = QueryAddedCallback(field,q);
		return q;
	}
}

Query* MultiFieldQueryParser::GetWildcardQuery(const TCHAR* field, TCHAR* termStr){
	if (field == NULL) {
		CL_NS_STD(vector)<BooleanClause*> clauses;
		for (int i = 0; fields[i]!=NULL; ++i) {
			Query* q = QueryParser::GetWildcardQuery(fields[i], termStr);
			if ( q ){
				q = QueryAddedCallback(fields[i],q);
				if ( q ){
					clauses.push_back(_CLNEW BooleanClause(q,true,false,false));
				}
			}
		}
		return QueryParser::GetBooleanQuery(clauses);
	}else{
		Query* q = QueryParser::GetWildcardQuery(field, termStr);
		if ( q )
			q = QueryAddedCallback(field,q);
		return q;
	}
}


Query* MultiFieldQueryParser::GetRangeQuery(const TCHAR* field, TCHAR* part1, TCHAR* part2, bool inclusive){
	if (field == NULL) {
		CL_NS_STD(vector)<BooleanClause*> clauses;
		for (int i = 0; fields[i]!=NULL; ++i) {
			Query* q = QueryParser::GetRangeQuery(fields[i], part1, part2, inclusive);
			if ( q ){
				q = QueryAddedCallback(fields[i],q);
				if ( q ){
					clauses.push_back(_CLNEW BooleanClause(q,true,false,false));
				}
			}
		}
		return QueryParser::GetBooleanQuery(clauses);
	}else{
		Query* q = QueryParser::GetRangeQuery(field, part1, part2, inclusive);
		if ( q )
			q = QueryAddedCallback(field,q);
		return q;
	}
}


CL_NS_END