Skip to content

Commit

Permalink
SERVER-9063 Add new match expression TextMatchExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
jrassi committed Oct 12, 2013
1 parent 5068233 commit 08fde2e
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/mongo/SConscript
Expand Up @@ -158,6 +158,11 @@ env.StaticLibrary('expressions_geo',
'db/matcher/expression_parser_geo.cpp'],
LIBDEPS=['expressions','geoquery','geoparser'] )

env.StaticLibrary('expressions_text',
['db/matcher/expression_text.cpp',
'db/matcher/expression_parser_text.cpp'],
LIBDEPS=['expressions','db/fts/base'] )

env.StaticLibrary('expressions_where',
['db/matcher/expression_where.cpp'],
LIBDEPS=['expressions'] )
Expand All @@ -174,6 +179,10 @@ env.CppUnitTest('expression_geo_test',
'db/matcher/expression_parser_geo_test.cpp'],
LIBDEPS=['expressions_geo'] )

env.CppUnitTest('expression_text_test',
['db/matcher/expression_parser_text_test.cpp'],
LIBDEPS=['expressions_text'] )

env.CppUnitTest('expression_parser_test',
['db/matcher/expression_parser_test.cpp',
'db/matcher/expression_parser_array_test.cpp',
Expand Down Expand Up @@ -373,6 +382,7 @@ env.StaticLibrary("coredb", [
'expressions',
'expressions_geo',
'expressions_where',
'expressions_text',
'db/exec/working_set',
'$BUILD_DIR/mongo/foundation',
'$BUILD_DIR/third_party/shim_snappy',
Expand Down
3 changes: 1 addition & 2 deletions src/mongo/db/matcher/expression.h
Expand Up @@ -62,8 +62,7 @@ namespace mongo {
ATOMIC, ALWAYS_FALSE,

// Things that we parse but cannot be answered without an index.
// TODO: Text goes here eventually.
GEO_NEAR,
GEO_NEAR, TEXT,
};

MatchExpression( MatchType type );
Expand Down
26 changes: 24 additions & 2 deletions src/mongo/db/matcher/expression_parser.cpp
Expand Up @@ -285,6 +285,17 @@ namespace mongo {
return s;
root->add( s.getValue() );
}
else if ( mongoutils::str::equals( "text", rest ) ) {
if ( e.type() != Object ) {
return StatusWithMatchExpression( ErrorCodes::BadValue,
"$text expects an object" );
}
StatusWithMatchExpression s = expressionParserTextCallback( e.Obj() );
if ( !s.isOK() ) {
return s;
}
root->add( s.getValue() );
}
else if ( mongoutils::str::equals( "comment", rest ) ) {
}
else {
Expand Down Expand Up @@ -623,19 +634,30 @@ namespace mongo {
return StatusWithMatchExpression( myAnd.release() );
}

// Geo
StatusWithMatchExpression expressionParserGeoCallbackDefault( const char* name,
int type,
const BSONObj& section ) {
return StatusWithMatchExpression( ErrorCodes::BadValue, "geo not linked in" );
}

MatchExpressionParserGeoCallback expressionParserGeoCallback = expressionParserGeoCallbackDefault;
MatchExpressionParserGeoCallback expressionParserGeoCallback =
expressionParserGeoCallbackDefault;

// Where
StatusWithMatchExpression expressionParserWhereCallbackDefault(const BSONElement& where) {
return StatusWithMatchExpression( ErrorCodes::BadValue, "$where not linked in" );
}

MatchExpressionParserWhereCallback expressionParserWhereCallback = expressionParserWhereCallbackDefault;
MatchExpressionParserWhereCallback expressionParserWhereCallback =
expressionParserWhereCallbackDefault;

// Text
StatusWithMatchExpression expressionParserTextCallbackDefault( const BSONObj& queryObj ) {
return StatusWithMatchExpression( ErrorCodes::BadValue, "$text not linked in" );
}

MatchExpressionParserTextCallback expressionParserTextCallback =
expressionParserTextCallbackDefault;

}
3 changes: 3 additions & 0 deletions src/mongo/db/matcher/expression_parser.h
Expand Up @@ -123,4 +123,7 @@ namespace mongo {
typedef boost::function<StatusWithMatchExpression(const BSONElement& where)> MatchExpressionParserWhereCallback;
extern MatchExpressionParserWhereCallback expressionParserWhereCallback;

typedef boost::function<StatusWithMatchExpression(const BSONObj& queryObj)> MatchExpressionParserTextCallback;
extern MatchExpressionParserTextCallback expressionParserTextCallback;

}
73 changes: 73 additions & 0 deletions src/mongo/db/matcher/expression_parser_text.cpp
@@ -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();
}

}
55 changes: 55 additions & 0 deletions src/mongo/db/matcher/expression_parser_text_test.cpp
@@ -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" );
}
}
88 changes: 88 additions & 0 deletions src/mongo/db/matcher/expression_text.cpp
@@ -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;
}

}
62 changes: 62 additions & 0 deletions src/mongo/db/matcher/expression_text.h
@@ -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
1 change: 1 addition & 0 deletions src/mongo/db/matcher/matcher.cpp
Expand Up @@ -260,6 +260,7 @@ namespace mongo {
case MatchExpression::GEO_NEAR:
case MatchExpression::NOT:
case MatchExpression::NOR:
case MatchExpression::TEXT:
// maybe?
return NULL;

Expand Down

0 comments on commit 08fde2e

Please sign in to comment.