Author: natalia Date: Tue Aug 21 18:58:31 2007 New Revision: 568372 URL: http://svn.apache.org/viewvc?rev=568372&view=rev Log: Configurable default field for text indexer
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/LuceneIndexer.java xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/LuceneIndexer.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/LuceneIndexer.java?rev=568372&r1=568371&r2=568372&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/LuceneIndexer.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/LuceneIndexer.java Tue Aug 21 18:58:31 2007 @@ -85,6 +85,7 @@ private static final String NAME = "name"; private static final String PATTERN = "pattern"; + private static final String DEFAULT = "default"; private static final String ANALYZER = "analyzer"; private static final String PATTERN_STRING = "pattern"; private static final String PATTERN_ALIAS = "alias"; @@ -112,6 +113,8 @@ private int docsAdded; private int docsDeleted; + private String defaultField = ""; + private void setFile(File f) { idxFile = f; } @@ -150,11 +153,18 @@ * org.apache.lucene.analysis.SimpleAnalyzer by default.</ul> * * <dl><dt>pattern - * <dd>Child element. Its attributes: - * <ul><li>pattern - IndexPattern. Required. For acceptable formats, see + * <dd>Child element. Indexer must have at least one pattern. Its + * attributes: + * <ul><li>pattern - IndexPattern. For acceptable formats, see * [EMAIL PROTECTED] org.apache.xindice.core.indexer.Indexer#getPatterns()} * <li>alias - Name of the field to store/search values for that pattern. - * Required.</ul></dl> + * </dl> + * <dl><dt>default + * <dd>Child element. Optional. Its attributes: + * <li>alias - Indicates the pattern alias that will be used as + * the default field for search. If omitted, search query has to include + * field name for all terms, there will be no default. + * </ul></dl> * </dl> * * @param config Configuration to apply @@ -182,6 +192,18 @@ this.patterns.put(new IndexPattern(collection.getSymbols(), name, null), alias); } + Configuration[] defaults = config.getChildren(DEFAULT); + if (defaults.length > 1) { + throw new CannotCreateException("There may be only one default field"); + } else if (defaults.length == 1) { + String alias = defaults[0].getAttribute(PATTERN_ALIAS); + if (this.patterns.values().contains(alias)) { + defaultField = alias; + } else { + throw new CannotCreateException("Alias '" + alias + "' is undefined in configuration"); + } + } + setFile(new File(collection.getCollectionRoot(), name)); } catch (Exception e) { throw new XindiceException(e); @@ -461,7 +483,7 @@ String textQuery = query.getValue(0).toString(); try { - return queryMatches(new QueryParser("", getAnalyzer()).parse(textQuery)); + return queryMatches(new QueryParser(defaultField, getAnalyzer()).parse(textQuery)); } catch (ParseException e) { throw new CompilationException("Failed to parse query '" + textQuery + "'", e); } Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java?rev=568372&r1=568371&r2=568372&view=diff ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/indexer/LuceneIndexerTest.java Tue Aug 21 18:58:31 2007 @@ -79,7 +79,7 @@ db.close(); } - private LuceneIndexer createIndex(String name, HashMap patterns) throws Exception { + private LuceneIndexer createIndex(String name, HashMap patterns, String defaultField) throws Exception { String config = "<index name='" + name + "' " + "class='" + indexClass + "' " + "analyzer='org.apache.lucene.analysis.SimpleAnalyzer'>"; @@ -87,6 +87,9 @@ String alias = (String) i.next(); config += "<pattern pattern='" + patterns.get(alias) + "' alias='" + alias + "' />"; } + if (defaultField != null) { + config += "<default alias='" + defaultField + "' />"; + } config += "</index>"; LuceneIndexer ind = (LuceneIndexer) collection.createIndexer(new Configuration(DOMParser.toDocument(config))); @@ -101,7 +104,7 @@ public void testSingleIndex() throws Exception { HashMap patterns = new HashMap(); patterns.put("test", "a"); - LuceneIndexer idx = createIndex("test", patterns); + LuceneIndexer idx = createIndex("test", patterns, null); String query = "test:test"; IndexMatch[] match = query(idx, query); @@ -114,9 +117,9 @@ HashMap patterns = new HashMap(); patterns.put("test1", "[EMAIL PROTECTED]"); patterns.put("test2", "[EMAIL PROTECTED]"); - createIndex("test1", patterns); - createIndex("test2", patterns); - assertTrue("There can be only one full text index per collection", false); + createIndex("test1", patterns, null); + createIndex("test2", patterns, null); + fail("There can be only one full text index per collection"); } catch (DuplicateIndexException e) { // correct behavior } @@ -126,27 +129,27 @@ HashMap patterns = new HashMap(); patterns.put("test1", "[EMAIL PROTECTED]"); patterns.put("test2", "[EMAIL PROTECTED]"); - LuceneIndexer idx = createIndex("test", patterns); + LuceneIndexer idx = createIndex("test", patterns, "test1"); - IndexMatch[] match = query(idx, "test1:test"); + IndexMatch[] match = query(idx, "test"); assertEquals(3, match.length); match = query(idx, "test2:test"); assertEquals(3, match.length); - match = query(idx, "test1:test AND test2:test"); + match = query(idx, "test AND test2:test"); assertEquals(1, match.length); - match = query(idx, "test1:test OR test2:test"); + match = query(idx, "test OR test2:test"); assertEquals(5, match.length); } public void testNestedContent() throws Exception { HashMap patterns = new HashMap(); patterns.put("doc", "doc"); - LuceneIndexer idx = createIndex("test", patterns); + LuceneIndexer idx = createIndex("test", patterns, "doc"); - String query = "doc:foo"; + String query = "foo"; IndexMatch[] match = query(idx, query); assertEquals(2, match.length);