SimpleQuery { public static Node parseTree(String query) throws IOException { List<QueryTerm> terms = parse(query); ArrayList<Node> nodes = new ArrayList<Node>(); for (QueryTerm term : terms) { Node termNode = new Node("text", term.text); if (term.field != null || term.text.contains(" ")) { TagTokenizer tokenizer = new TagTokenizer(); Document document = new Document(); document.text = term.text; tokenizer.process(document); String[] phraseTerms = document.terms.toArray(new String[0]); if (phraseTerms.length > 1) { ArrayList<Node> children = new ArrayList<Node>(); for (String phraseTerm : phraseTerms) { children.add(new Node("text", phraseTerm)); } NodeParameters np = new NodeParameters(); np.set("default", 1); termNode = new Node("ordered", np, children, 0); } else { termNode = new Node("text", phraseTerms[0]); } } if (term.field != null) { ArrayList<Node> children = new ArrayList<Node>(); children.add(termNode); children.add(new Node("field", term.field)); termNode = new Node("inside", children); } if (term.weight != 1.0) { ArrayList<Node> children = new ArrayList<Node>(); children.add(termNode); NodeParameters np = new NodeParameters(); np.set("default", term.weight); termNode = new Node("scale", np, children, 0); } nodes.add(termNode); } if (nodes.size() < 1) { return null; } return new Node("combine", nodes); }  static QueryTerm parseQueryTerm(String term); static List<String> textQueryTerms(String query); static List<QueryTerm> parse(String _query); static Node parseTree(String query); static final Pattern fixFieldQuery; }
@Test public void testSingleTerm() throws IOException { Node result = SimpleQuery.parseTree("a"); assertEquals("#combine( #text:a() )", result.toString()); }
@Test public void testManyTerms() throws IOException { Node result = SimpleQuery.parseTree("a b c"); assertEquals("#combine( #text:a() #text:b() #text:c() )", result.toString()); }
@Test public void testScale() throws IOException { Node result = SimpleQuery.parseTree("a^2"); assertEquals("#combine( #scale:2.0( #text:a() ) )", result.toString()); }
@Test public void testField() throws IOException { Node result = SimpleQuery.parseTree("f:a"); assertEquals("#combine( #inside( #text:a() #field:f() ) )", result.toString()); }
@Test public void testFieldWithSpace() throws IOException { Node result = SimpleQuery.parseTree("f: a"); assertEquals("#combine( #inside( #text:a() #field:f() ) )", result.toString()); }
@Test public void testFieldsWithSpaceAndTabs() throws IOException { Node result = SimpleQuery.parseTree("f: a f: b"); assertEquals("#combine( #inside( #text:a() #field:f() ) #inside( #text:b() #field:f() ) )", result.toString()); }
@Test public void testPhrase() throws IOException { Node result = SimpleQuery.parseTree("\"a b\""); assertEquals("#combine( #ordered:1( #text:a() #text:b() ) )", result.toString()); }
@Test public void testComplex() throws IOException { Node result = SimpleQuery.parseTree("fi:\"a c\""); assertEquals( "#combine( #inside( #ordered:1( #text:a() #text:c() ) #field:fi() ) )", result.toString()); }
@Test public void testComplex2() throws IOException { Node result = SimpleQuery.parseTree("fi:\"a c\"^7"); assertEquals( "#combine( #scale:7.0( #inside( #ordered:1( #text:a() #text:c() ) #field:fi() ) ) )", result.toString()); }
@Test public void testFieldWithEmail() throws IOException { Node result = SimpleQuery.parseTree("email:\"michaelz@cs.umass.edu\""); String expectedResult = "#combine( #inside( #ordered:1( #text:michaelz() #text:cs() #text:umass() #text:edu() ) #field:email() ) )"; assertEquals(expectedResult, result.toString()); result = SimpleQuery.parseTree("email:michaelz@cs.umass.edu"); assertEquals(expectedResult, result.toString()); }
@Test public void testFieldWithPunctuation() throws IOException { Node result = SimpleQuery.parseTree("author:\"W. Bruce Croft\""); String expectedResult = "#combine( #inside( #ordered:1( #text:w() #text:bruce() #text:croft() ) #field:author() ) )"; assertEquals(expectedResult, result.toString()); }