Add terms execute for now
Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/af68cd30 Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/af68cd30 Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/af68cd30 Branch: refs/heads/master Commit: af68cd304be39e650ffbaf2b56d682215d880451 Parents: 4ddfab1 Author: twilliams <[email protected]> Authored: Mon Sep 22 22:15:48 2014 -0400 Committer: twilliams <[email protected]> Committed: Mon Sep 22 22:15:48 2014 -0400 ---------------------------------------------------------------------- .../org/apache/blur/command/TermsCommand.java | 110 +++++++++++++++++++ .../apache/blur/command/TermsCommandTest.java | 82 ++++++++++++++ 2 files changed, 192 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/af68cd30/blur-command/src/main/java/org/apache/blur/command/TermsCommand.java ---------------------------------------------------------------------- diff --git a/blur-command/src/main/java/org/apache/blur/command/TermsCommand.java b/blur-command/src/main/java/org/apache/blur/command/TermsCommand.java new file mode 100644 index 0000000..be0d5f5 --- /dev/null +++ b/blur-command/src/main/java/org/apache/blur/command/TermsCommand.java @@ -0,0 +1,110 @@ +package org.apache.blur.command; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.blur.utils.BlurUtil; +import org.apache.lucene.index.AtomicReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.index.TermsEnum.SeekStatus; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TopDocs; +import org.apache.lucene.util.BytesRef; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 TermsCommand extends Command implements ClusterCommand<List<String>>, + IndexReadCombiningCommand<List<String>, List<String>> { + private static final String NAME = "terms"; + private static final String PARAMS = "params"; + private static final String P_SIZE = "size"; + private static final String P_FIELD = "fieldName"; + private static final String P_START = "startWith"; + + private static final short DEFAULT_SIZE = 10; + + @Override + public List<String> execute(IndexContext context) throws IOException { + BlurObject params = context.getArgs().get(PARAMS); + short size = params.getShort(P_SIZE, DEFAULT_SIZE); + String fieldName = params.get(P_FIELD); + String startWith = params.getString(P_START, ""); + + return terms(context.getIndexReader(), fieldName, startWith, size); + } + + @Override + public List<String> combine(Map<Shard, List<String>> results) throws IOException { + return null; + } + + @Override + public List<String> clusterExecute(ClusterContext context) throws IOException { + return null; + } + + @Override + public String getName() { + return NAME; + } + + private static List<String> terms(IndexReader reader, String fieldName, String startWith, short size) + throws IOException { + + Term term = getTerm(fieldName, startWith); + List<String> terms = new ArrayList<String>(size); + AtomicReader areader = BlurUtil.getAtomicReader(reader); + Terms termsAll = areader.terms(term.field()); + + if (termsAll == null) { + return terms; + } + + TermsEnum termEnum = termsAll.iterator(null); + + SeekStatus status = termEnum.seekCeil(term.bytes()); + + if (status == SeekStatus.END) { + return terms; + } + + BytesRef currentTermText = termEnum.term(); + do { + terms.add(currentTermText.utf8ToString()); + if (terms.size() >= size) { + return terms; + } + } while ((currentTermText = termEnum.next()) != null); + return terms; + } + + private static Term getTerm(String fieldName, String value) { + if (fieldName == null) { + throw new NullPointerException("fieldName cannot be null."); + } + + return new Term(fieldName, value); + + } + +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/af68cd30/blur-command/src/test/java/org/apache/blur/command/TermsCommandTest.java ---------------------------------------------------------------------- diff --git a/blur-command/src/test/java/org/apache/blur/command/TermsCommandTest.java b/blur-command/src/test/java/org/apache/blur/command/TermsCommandTest.java new file mode 100644 index 0000000..227acee --- /dev/null +++ b/blur-command/src/test/java/org/apache/blur/command/TermsCommandTest.java @@ -0,0 +1,82 @@ +package org.apache.blur.command; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.google.common.collect.Lists; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 TermsCommandTest { + private static IndexContext ctx; + + @BeforeClass + public static void init() { + ctx = CoreTestContext.newSimpleAlpaNumContext(); + } + + @Test + public void basicTermsShouldReturn() throws IOException { + List<String> returned = getExecuteResult(newContext("val", null, null)); + List<String> expected = Lists.newArrayList("val"); + + assertEquals(expected, returned); + } + + @Test + public void sizeOfTermsRequestShouldBeRespected() throws IOException { + List<String> returned = getExecuteResult(newContext("alpha", (short) 7, null)); + List<String> expected = Lists.newArrayList("aa", "bb", "cc", "dd", "ee", "ff", "gg"); + + assertEquals(expected, returned); + } + + @Test + public void sizeShouldDefaultToTen() throws IOException { + List<String> returned = getExecuteResult(newContext("alpha", null, null)); + List<String> expected = Lists.newArrayList("aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj"); + + assertEquals(expected, returned); + } + + private List<String> getExecuteResult(IndexContext context) throws IOException { + TermsCommand cmd = new TermsCommand(); + return cmd.execute(context); + } + + private IndexContext newContext(String field, Short size, String startsWith) { + + Args args = new Args(); + BlurObject params = new BlurObject(); + params.put("fieldName", field); + + if (size != null) { + params.put("size", size); + } + if (startsWith != null) { + params.put("startWith", startsWith); + } + args.set("params", params); + + return new TestContextArgDecorator(ctx, args); + } +}
