DATAMONGO-937 - Add support for creating text index.

We now support creating text index based on information gathered on domain types.

Using @TextIndexed marks properties to be considered for the full text index. Use the weight attribute to influence document scoring during search operations.

Please note that using @TextIndexed on entity properties forces all properties of any sub document to be considered as part of the text index. Any set weight will in that case be propagated to the siblings taking the most recent weight information into account, which means that a the weight attribute can be overridden for properties in sub documents.

The setting the index default language can be done via @Document(language) while @Language can be used to define the language_override field.

As text search is disabled by default for mongodb 2.4 we use a jUnit ClassRule to restrict integration tests potentially creating text index (as the entities for testing are found in the classpath) to only be executed in when a 2.6+ mongodb server is present.

For usage hints please see section 6.3.4 (Text Indexes) of reference manual.

Original pull request: #198.
This commit is contained in:
Christoph Strobl
2014-06-24 11:31:24 +02:00
committed by Thomas Darimont
parent 998bb09a92
commit 84913cecab
22 changed files with 1210 additions and 46 deletions

View File

@@ -357,6 +357,16 @@ public class Person {
level to describe how to geoindex the field.</para>
</listitem>
<listitem>
<para><literal>@TextIndexed</literal> - applied at the field level
to mark the field to be included in the text index.</para>
</listitem>
<listitem>
<para><literal>@Language</literal> - applied at the field level to
set the language override property for text index.</para>
</listitem>
<listitem>
<para><literal>@Transient</literal> - by default all private fields
are mapped to the document, this annotation excludes the field where
@@ -526,7 +536,7 @@ OrderItem item = converter.read(OrderItem.class, input);</programlisting>
test suite.</para>
</section>
<section id="mapping-usage-indexes">
<section id="mapping-usage-indexes.compound-index">
<title>Compound Indexes</title>
<para>Compound indexes are also supported. They are defined at the class
@@ -561,6 +571,51 @@ public class Person {
</example></para>
</section>
<section id="mapping-usage-indexes.text-index">
<title>Text Indexes</title>
<note>
<para>The text index feature is disabled by default for mongodb
v.2.4.</para>
</note>
<para>Creating a text index allows to accumulate several fields into a
searchable full text index. It is only possible to have one text index
per collection so all fields marked with
<interfacename>@TextIndexed</interfacename> are combined into this
index. Properties can be weighted to influence document score for
ranking results. The default language for the text index is english, to
change the default language set
<interfacename>@Document(language="spanish")</interfacename> to any
language you want. Using a property called <literal>language</literal>
or <interfacename>@Language</interfacename> allows to define a language
override on a per document base.</para>
<example>
<title>Example Text Index Usage</title>
<programlisting language="java">@Document(language = "spanish")
class SomeEntity {
@TextIndexed String foo;
@Language String lang;
Nested nested;
}
class Nested {
@TextIndexed(weight=5) String bar;
String roo;
}
</programlisting>
</example>
</section>
<section id="mapping-usage-references">
<title>Using DBRefs</title>

View File

@@ -3046,11 +3046,12 @@ class MyConverter implements Converter&lt;String, Person&gt; { … }</programlis
</listitem>
</itemizedlist></para>
<para>You can create both standard indexes and geospatial indexes using
the classes <classname>IndexDefinition</classname> and
<classname>GeoSpatialIndex</classname> respectfully. For example, given
the Venue class defined in a previous section, you would declare a
geospatial query as shown below</para>
<para>You can create standard, geospatial and text indexes using the
classes <classname>IndexDefinition</classname>,
<classname>GeoSpatialIndex</classname> and
<classname>TextIndexDefinition</classname>. For example, given the Venue
class defined in a previous section, you would declare a geospatial
query as shown below.</para>
<programlisting language="java">mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));</programlisting>
</section>