lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject lucy git commit: Document how to create TermQuery for FullTextType
Date Sun, 18 Jan 2015 13:05:45 GMT
Repository: lucy
Updated Branches:
  refs/heads/master 2bc4edbd9 -> 85eb501f0


Document how to create TermQuery for FullTextType


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/85eb501f
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/85eb501f
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/85eb501f

Branch: refs/heads/master
Commit: 85eb501f0303bc58198b809ac10e55baa4acc298
Parents: 2bc4edb
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sun Jan 18 14:03:52 2015 +0100
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sun Jan 18 14:03:52 2015 +0100

----------------------------------------------------------------------
 core/Lucy/Plan/FullTextType.cfh              |  2 ++
 perl/buildlib/Lucy/Build/Binding/Analysis.pm |  5 +++
 perl/buildlib/Lucy/Build/Binding/Plan.pm     |  1 +
 perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod | 39 +++++++++++++++++++++++
 4 files changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/85eb501f/core/Lucy/Plan/FullTextType.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Plan/FullTextType.cfh b/core/Lucy/Plan/FullTextType.cfh
index a98a1e7..f793294 100644
--- a/core/Lucy/Plan/FullTextType.cfh
+++ b/core/Lucy/Plan/FullTextType.cfh
@@ -65,6 +65,8 @@ public class Lucy::Plan::FullTextType inherits Lucy::Plan::TextType {
     public bool
     Highlightable(FullTextType *self);
 
+    /** Accessor for the type's analyzer.
+     */
     public Analyzer*
     Get_Analyzer(FullTextType *self);
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/85eb501f/perl/buildlib/Lucy/Build/Binding/Analysis.pm
----------------------------------------------------------------------
diff --git a/perl/buildlib/Lucy/Build/Binding/Analysis.pm b/perl/buildlib/Lucy/Build/Binding/Analysis.pm
index d90909b..82745fe 100644
--- a/perl/buildlib/Lucy/Build/Binding/Analysis.pm
+++ b/perl/buildlib/Lucy/Build/Binding/Analysis.pm
@@ -35,8 +35,13 @@ sub bind_all {
 }
 
 sub bind_analyzer {
+    my @exposed = qw(
+        Split
+    );
+
     my $pod_spec = Clownfish::CFC::Binding::Perl::Pod->new;
     $pod_spec->set_synopsis("    # Abstract base class.\n");
+    $pod_spec->add_method( method => $_, alias => lc($_) ) for @exposed;
 
     my $binding = Clownfish::CFC::Binding::Perl::Class->new(
         parcel     => "Lucy",

http://git-wip-us.apache.org/repos/asf/lucy/blob/85eb501f/perl/buildlib/Lucy/Build/Binding/Plan.pm
----------------------------------------------------------------------
diff --git a/perl/buildlib/Lucy/Build/Binding/Plan.pm b/perl/buildlib/Lucy/Build/Binding/Plan.pm
index c094b97..461bfbf 100644
--- a/perl/buildlib/Lucy/Build/Binding/Plan.pm
+++ b/perl/buildlib/Lucy/Build/Binding/Plan.pm
@@ -184,6 +184,7 @@ sub bind_fulltexttype {
     my @exposed = qw(
         Set_Highlightable
         Highlightable
+        Get_Analyzer
     );
 
     my $pod_spec = Clownfish::CFC::Binding::Perl::Pod->new;

http://git-wip-us.apache.org/repos/asf/lucy/blob/85eb501f/perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod
----------------------------------------------------------------------
diff --git a/perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod b/perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod
index 0a1802e..6ff812a 100644
--- a/perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod
+++ b/perl/lib/Lucy/Docs/Tutorial/QueryObjects.pod
@@ -143,6 +143,45 @@ Now when we execute the query...
 ... we'll get a result set which is the intersection of the parsed query and
 the category query.
 
+=head1 Using TermQuery with full text fields
+
+When querying full text fields, the easiest way is to create query objects
+using QueryParser. But sometimes you want to create TermQuery for a single
+term in a FullTextType field directly. In this case, we have to run the
+search term through the field's analyzer to make sure it gets normalized in
+the same way as the field's content.
+
+    sub make_term_query {
+        my ($field, $term) = @_;
+
+        my $token;
+        my $type = $schema->fetch_type($field);
+
+        if ( $type->isa('Lucy::Plan::FullTextType') ) {
+            # Run the term through the full text analysis chain.
+            my $analyzer = $type->get_analyzer;
+            my $tokens   = $analyzer->split($term);
+
+            if ( @$tokens != 1 ) {
+                # If the term expands to more than one token, or no
+                # tokens at all, it will never match a token in the
+                # full text field.
+                return Lucy::Search::NoMatchQuery->new;
+            }
+
+            $token = $tokens->[0];
+        }
+        else {
+            # Exact match for other types.
+            $token = $term;
+        }
+
+        return Lucy::Search::TermQuery->new(
+            field => $field,
+            term  => $token,
+        );
+    }
+
 =head1 Congratulations!
 
 You've made it to the end of the tutorial.


Mime
View raw message