Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SERVER-9063 Add new match expression TextMatchExpression
- Loading branch information
Showing
9 changed files
with
317 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// expression_parser_text.cpp | ||
|
||
/** | ||
* Copyright (C) 2013 10gen Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the GNU Affero General Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#include "mongo/base/init.h" | ||
#include "mongo/db/client.h" | ||
#include "mongo/db/index/catalog_hack.h" | ||
#include "mongo/db/jsobj.h" | ||
#include "mongo/db/matcher/expression_parser.h" | ||
#include "mongo/db/matcher/expression_text.h" | ||
#include "mongo/db/namespace_details.h" | ||
|
||
namespace mongo { | ||
|
||
StatusWithMatchExpression expressionParserTextCallbackReal( const BSONObj& queryObj ) { | ||
// Validate queryObj, but defer construction of FTSQuery (which requires access to the | ||
// target namespace) until stage building time. | ||
|
||
if ( mongo::String != queryObj["$search"].type() ) { | ||
return StatusWithMatchExpression( ErrorCodes::BadValue, "$search needs a String" ); | ||
} | ||
|
||
BSONElement languageElt = queryObj["$language"]; | ||
if ( !languageElt.eoo() && mongo::String != languageElt.type() ) { | ||
return StatusWithMatchExpression( ErrorCodes::BadValue, "$language needs a String" ); | ||
} | ||
string language = ( !languageElt.eoo() ? languageElt.String() : "" ); | ||
string query = queryObj["$search"].String(); | ||
|
||
if ( queryObj.nFields() != ( languageElt.eoo() ? 1 : 2 ) ) { | ||
return StatusWithMatchExpression( ErrorCodes::BadValue, "extra fields in $text" ); | ||
} | ||
|
||
auto_ptr<TextMatchExpression> e( new TextMatchExpression() ); | ||
Status s = e->init( query, language ); | ||
if ( !s.isOK() ) { | ||
return StatusWithMatchExpression( s ); | ||
} | ||
return StatusWithMatchExpression( e.release() ); | ||
} | ||
|
||
MONGO_INITIALIZER( MatchExpressionParserText )( ::mongo::InitializerContext* context ) { | ||
expressionParserTextCallback = expressionParserTextCallbackReal; | ||
return Status::OK(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// expression_parser_text_test.cpp | ||
|
||
/** | ||
* Copyright (C) 2013 10gen Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the GNU Affero General Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#include "mongo/unittest/unittest.h" | ||
|
||
#include "mongo/db/matcher/expression_parser.h" | ||
|
||
#include "mongo/db/jsobj.h" | ||
#include "mongo/db/json.h" | ||
#include "mongo/db/matcher/expression.h" | ||
#include "mongo/db/matcher/expression_text.h" | ||
|
||
namespace mongo { | ||
|
||
TEST( MatchExpressionParserText, Text ) { | ||
BSONObj query = fromjson( "{$text:{$search:\"awesome\", $language:\"english\"}}" ); | ||
|
||
StatusWithMatchExpression result = MatchExpressionParser::parse( query ); | ||
ASSERT_TRUE( result.isOK() ); | ||
|
||
MatchExpression* exp = result.getValue(); | ||
ASSERT_EQUALS( MatchExpression::TEXT, exp->matchType() ); | ||
|
||
TextMatchExpression* textExp = static_cast<TextMatchExpression*>( exp ); | ||
ASSERT_EQUALS( textExp->getQuery(), "awesome" ); | ||
ASSERT_EQUALS( textExp->getLanguage(), "english" ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// expression_text.cpp | ||
|
||
/** | ||
* Copyright (C) 2013 10gen Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the GNU Affero General Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#include "mongo/pch.h" | ||
#include "mongo/db/matcher/expression_text.h" | ||
#include "mongo/db/query/new_find.h" | ||
|
||
namespace mongo { | ||
|
||
Status TextMatchExpression::init( const string& query, const string& language ) { | ||
_query = query; | ||
_language = language; | ||
return initPath( "_fts" ); | ||
} | ||
|
||
bool TextMatchExpression::matchesSingleElement( const BSONElement& e ) const { | ||
// This shouldn't be called. | ||
verify(0); | ||
return false; | ||
} | ||
|
||
void TextMatchExpression::debugString( StringBuilder& debug, int level ) const { | ||
_debugAddSpace(debug, level); | ||
debug << "TEXT : query=" << _query << ", language = " << _language << ", tag="; | ||
MatchExpression::TagData* td = getTag(); | ||
if ( NULL != td ) { | ||
td->debugString( &debug ); | ||
} | ||
else { | ||
debug << "NULL"; | ||
} | ||
debug << "\n"; | ||
} | ||
|
||
bool TextMatchExpression::equivalent( const MatchExpression* other ) const { | ||
if ( matchType() != other->matchType() ) { | ||
return false; | ||
} | ||
const TextMatchExpression* realOther = static_cast<const TextMatchExpression*>( other ); | ||
|
||
// TODO This is way too crude. It looks for string equality, but it should be looking for | ||
// common parsed form | ||
if ( realOther->getQuery() != _query ) { | ||
return false; | ||
} | ||
if ( realOther->getLanguage() != _language ) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
LeafMatchExpression* TextMatchExpression::shallowClone() const { | ||
TextMatchExpression* next = new TextMatchExpression(); | ||
next->init( _query, _language ); | ||
if ( getTag() ) { | ||
next->setTag( getTag()->clone() ); | ||
} | ||
return next; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// expression_text.h | ||
|
||
/** | ||
* Copyright (C) 2013 10gen Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the GNU Affero General Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "mongo/db/fts/fts_query.h" | ||
#include "mongo/db/matcher/expression.h" | ||
#include "mongo/db/matcher/expression_leaf.h" | ||
|
||
namespace mongo { | ||
|
||
class TextMatchExpression : public LeafMatchExpression { | ||
public: | ||
TextMatchExpression() : LeafMatchExpression( TEXT ) {} | ||
virtual ~TextMatchExpression() {} | ||
|
||
Status init( const std::string& query, const std::string& language ); | ||
|
||
// This shouldn't be called and as such will crash. GeoNear always requires an index. | ||
virtual bool matchesSingleElement( const BSONElement& e ) const; | ||
|
||
virtual void debugString( StringBuilder& debug, int level = 0 ) const; | ||
|
||
virtual bool equivalent( const MatchExpression* other ) const; | ||
|
||
virtual LeafMatchExpression* shallowClone() const; | ||
|
||
const string& getQuery() const { return _query; } | ||
const string& getLanguage() const { return _language; } | ||
private: | ||
std::string _query; | ||
std::string _language; | ||
}; | ||
|
||
} // namespace mongo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters