Skip to content

Commit

Permalink
add int/double point parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnfeldman committed Nov 1, 2017
1 parent b9bd2b7 commit fff096c
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ bin/
.project
.classpath
.settings/
.vscode
Dockerfile
docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.co.flax.luwak.queryparsers;

/**
* Copyright (c) 2013 Lemur Consulting Ltd.
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.HashMap;


import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
import org.apache.lucene.queryparser.flexible.standard.config.PointsConfig;
import org.apache.lucene.search.Query;
import uk.co.flax.luwak.MonitorQueryParser;

/**
* A query parser that uses the default Lucene parser
*/
public class FlexibleLuceneQueryParser implements MonitorQueryParser {

private final String defaultField;
private final Analyzer analyzer;
private final Map<String,PointsConfig> pointsConfig;
private final StandardQueryParser parser;

/**
* Creates a parser with a given default field and analyzer
* @param defaultField the default field
* @param analyzer an analyzer to use to analyzer query terms
* @param pointsConfig configures pointfields like this https://github.com/apache/lucene-solr/blob/master/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestPointQueryParser.java
*/
public FlexibleLuceneQueryParser(String defaultField, Analyzer analyzer, Map<String,PointsConfig> pointsConfig) {
this.defaultField = defaultField;
this.analyzer = analyzer;
this.pointsConfig = pointsConfig;
this.parser = new StandardQueryParser(this.analyzer);
}

/**
* Creates a parser using a lucene {@link StandardAnalyzer}
* @param defaultField the default field
* @param analyzer an analyzer to use to analyzer query terms
*/
public FlexibleLuceneQueryParser(String defaultField, Analyzer analyzer){
this(defaultField, analyzer,new HashMap<>());
}

/**
* Creates a parser using a lucene {@link StandardAnalyzer}
* @param defaultField the default field
*/
public FlexibleLuceneQueryParser(String defaultField) {
this(defaultField, new StandardAnalyzer(),new HashMap<>());
}

/**
* returns internal points config
*/
public Map<String,PointsConfig> getPointsConfig(){
return this.pointsConfig;
}

/**
* return the StandardQueryParser
*/
public StandardQueryParser getParser(){
return this.parser;
}

@Override
public Query parse(String query, Map<String, String> metadata) throws Exception {
if (this.pointsConfig.size() > 0) {
parser.setPointsConfigMap(pointsConfig);
}
return parser.parse(query,this.defaultField);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package uk.co.flax.luwak;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.search.Query;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import junit.framework.Assert;
import uk.co.flax.luwak.matchers.SimpleMatcher;
import uk.co.flax.luwak.presearcher.MatchAllPresearcher;
import uk.co.flax.luwak.queryparsers.LuceneQueryParser;
import uk.co.flax.luwak.queryparsers.FlexibleLuceneQueryParser;

import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
import org.apache.lucene.queryparser.flexible.standard.config.PointsConfig;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery.Builder;

import static uk.co.flax.luwak.assertions.MatchesAssert.assertThat;

/**
* Copyright (c) 2013 Lemur Consulting Ltd.
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class TestMonitorFlexibleLuceneQueryParser{

static final String TEXTFIELD = "TEXTFIELD";

static final Analyzer ANALYZER = new WhitespaceAnalyzer();

private Monitor monitor;
private FlexibleLuceneQueryParser parser;
@Before
public void setUp() throws IOException {
parser = new FlexibleLuceneQueryParser(TEXTFIELD, ANALYZER);
monitor = new Monitor(parser, new MatchAllPresearcher());
}

@Test
public void matchIntPointfields() throws IOException,UpdateException,Exception {

parser.getPointsConfig().put("age",new PointsConfig(NumberFormat.getIntegerInstance(Locale.ROOT), Integer.class));


InputDocument doc = InputDocument.builder("doc1")
.addField(new IntPoint("age",1))
.addField(new IntPoint("somethingelse",2))
.build();

MonitorQuery mq = new MonitorQuery("query1","(age:1 somethingelse:2)");
monitor.update(mq);
Matches<?> matches = monitor.match(doc, SimpleMatcher.FACTORY);
assertThat(matches)
.hasMatchCount("doc1", 1);

mq = new MonitorQuery("query1","age:1");
monitor.update(mq);
matches = monitor.match(doc, SimpleMatcher.FACTORY);
assertThat(matches)
.hasMatchCount("doc1", 1);
}

@Test
public void matchDoublePointfields() throws IOException,UpdateException,Exception {

parser.getPointsConfig().put("money",new PointsConfig(NumberFormat.getNumberInstance(Locale.ROOT), Double.class));
parser.getPointsConfig().put("somethingelse",new PointsConfig(NumberFormat.getIntegerInstance(Locale.ROOT), Integer.class));

InputDocument doc = InputDocument.builder("doc1")
.addField(new DoublePoint("money",1.0))
.addField(new IntPoint("somethingelse",2))
.build();

MonitorQuery mq = new MonitorQuery("query1","(money:1.0 somethingelse:2)");
monitor.update(mq);
Matches<?> matches = monitor.match(doc, SimpleMatcher.FACTORY);
assertThat(matches)
.hasMatchCount("doc1", 1);

mq = new MonitorQuery("query1","money:1");
monitor.update(mq);
matches = monitor.match(doc, SimpleMatcher.FACTORY);
assertThat(matches)
.hasMatchCount("doc1", 1);
}

}

0 comments on commit fff096c

Please sign in to comment.