lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markharw00d <markharw...@yahoo.co.uk>
Subject Re: query syntax question
Date Thu, 10 May 2007 21:07:05 GMT
Here's a way to do it using the XML query parser in contrib....
1) Create this query.xsl file (note use of cached double negative filter)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Document">
<FilteredQuery>
    <Query>
        <UserQuery><xsl:value-of select="query"/></UserQuery>
    </Query>
    <Filter>
        <CachedFilter>
            <BooleanFilter>
                <Clause occurs="mustNot">
                    <RangeFilter fieldName="surname" lowerTerm="a" 
upperTerm="z"/>
                </Clause>
                <Clause occurs="mustNot">
                    <RangeFilter fieldName="forename" lowerTerm="a" 
upperTerm="z"/>
                </Clause>
            </BooleanFilter>
        </CachedFilter>
    </Filter>
</FilteredQuery>
</xsl:template>
</xsl:stylesheet> 

2) Query as follows:
        //Setup test data
        Analyzer analyzer=new WhitespaceAnalyzer();       
        RAMDirectory rd=new RAMDirectory();
        IndexWriter w=new IndexWriter(rd,new WhitespaceAnalyzer(),true);
        Document d=new Document();
        d.add(new Field("contents","foo 1- must not 
match",Field.Store.YES,Field.Index.TOKENIZED));
        d.add(new 
Field("surname","smith",Field.Store.YES,Field.Index.TOKENIZED));
        w.addDocument(d);
       
        d=new Document();
        d.add(new Field("contents","foo 2- must not 
match",Field.Store.YES,Field.Index.TOKENIZED));
        d.add(new 
Field("forename","fred",Field.Store.YES,Field.Index.TOKENIZED));
        w.addDocument(d);
       
        d=new Document();
        d.add(new Field("contents","foo 3- must not 
match",Field.Store.YES,Field.Index.TOKENIZED));
        d.add(new 
Field("forename","fred",Field.Store.YES,Field.Index.TOKENIZED));
        d.add(new 
Field("surname","smith",Field.Store.YES,Field.Index.TOKENIZED));
        w.addDocument(d);
       
        d=new Document();
        d.add(new Field("contents","foo 4- must 
match",Field.Store.YES,Field.Index.TOKENIZED));
        w.addDocument(d);
        w.close();
       
        IndexSearcher searcher=new IndexSearcher(rd);
       
        //one-off setup - store these
        QueryTemplateManager qtm=new QueryTemplateManager(
                TestXml.class.getResourceAsStream("query.xsl"));
        CorePlusExtensionsParser cp = new CorePlusExtensionsParser(analyzer,
                new QueryParser("contents",analyzer));
       
         //get the user form input
        String queryString="foo";
        Properties userInput=new Properties();
        userInput.setProperty("query",queryString);

//        Transform the user input into a Lucene XML query, then pass to 
parser
        Query 
q=cp.getQuery(qtm.getQueryAsDOM(userInput).getDocumentElement());
       
        Hits h = searcher.search(q);
        for (int i = 0; i < h.length(); i++)
        {
            d=h.doc(i);
            System.out.println(d.get("contents"));
        }

Cheers
Mark




---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message