Exemplo n.º 1
0
 @Override
 public BytesRef indexedValueForSearch(Object value) {
   long longValue = NumericUtils.doubleToSortableLong(parseDoubleValue(value));
   BytesRefBuilder bytesRef = new BytesRefBuilder();
   NumericUtils.longToPrefixCoded(longValue, 0, bytesRef); // 0 because of exact match
   return bytesRef.get();
 }
 @Override
 public void fillBytesRef() {
   assert valueSize == 64 || valueSize == 32;
   if (valueSize == 64) {
     NumericUtils.longToPrefixCoded(value, shift, bytes);
   } else {
     NumericUtils.intToPrefixCoded((int) value, shift, bytes);
   }
 }
 /**
  * Converts a list of long value to their bytes ref representation as performed by {@link
  * org.apache.lucene.analysis.NumericTokenStream}
  */
 private BytesRef[] toBytesRefs(long[] values) {
   BytesRef[] bytesRefs = new BytesRef[values.length];
   for (int i = 0; i < values.length; i++) {
     BytesRefBuilder b = new BytesRefBuilder();
     NumericUtils.longToPrefixCoded(values[i], 0, b);
     bytesRefs[i] = b.toBytesRef();
   }
   return bytesRefs;
 }
 @Override
 public BytesRef getBytesRef() {
   assert valueSize == 64 || valueSize == 32;
   if (valueSize == 64) {
     NumericUtils.longToPrefixCoded(value, shift, bytes);
   } else {
     NumericUtils.intToPrefixCoded((int) value, shift, bytes);
   }
   return bytes.get();
 }
Exemplo n.º 5
0
 @Override
 public String readableToIndexed(String val) {
   switch (type) {
     case INTEGER:
       return NumericUtils.intToPrefixCoded(Integer.parseInt(val));
     case FLOAT:
       return NumericUtils.intToPrefixCoded(
           NumericUtils.floatToSortableInt(Float.parseFloat(val)));
     case LONG:
       return NumericUtils.longToPrefixCoded(Long.parseLong(val));
     case DOUBLE:
       return NumericUtils.longToPrefixCoded(
           NumericUtils.doubleToSortableLong(Double.parseDouble(val)));
     case DATE:
       return NumericUtils.longToPrefixCoded(dateField.parseMath(null, val).getTime());
     default:
       throw new SolrException(
           SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
   }
 }
  /**
   * Test that the we are able to index a 100 documents, that the job management works during that
   * time and at least one of the submissions exists in the index afterwards.
   */
  @Test
  public void testCommit() throws CorruptIndexException, IOException, InterruptedException {

    // Send the first 100 subissions to the current transaction.
    Iterator<Submission> itr = subRepo.findAllSubmissions();

    long lastSubId = 0;
    for (int i = 0; i < 100; i++) {
      Submission sub = itr.next();
      assertNotNull(sub);
      indexer.updated(sub);

      lastSubId = sub.getId();
    }

    // Assert no job is running.
    assertTrue(lastSubId > 0);
    assertFalse(indexer.isJobRunning());
    assertEquals("None", indexer.getCurrentJobLabel());
    assertEquals(-1L, indexer.getCurrentJobProgress());
    assertEquals(-1L, indexer.getCurrentJobTotal());
    assertTrue(indexer.isUpdated(lastSubId));

    // Commit the 100 jobs.
    indexer.commit(false);

    // Check that a job is running
    assertTrue(indexer.isJobRunning());
    assertEquals("Update Index", indexer.getCurrentJobLabel());
    assertTrue(indexer.getCurrentJobProgress() >= 0 && indexer.getCurrentJobProgress() <= 100);
    assertEquals(100L, indexer.getCurrentJobTotal());

    // Wait for the current job to complete
    for (int i = 0; i < 1000; i++) {
      Thread.sleep(10);
      if (!indexer.isJobRunning()) break;
    }
    assertFalse(indexer.isJobRunning());

    // Check that we can find the documents we indexed.
    IndexReader reader = IndexReader.open(indexer.index);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermQuery(new Term("subId", NumericUtils.longToPrefixCoded(lastSubId)));

    TopDocs topDocs = searcher.search(query, 1);
    assertNotNull(topDocs);
    assertNotNull(topDocs.scoreDocs);
    assertTrue(topDocs.scoreDocs.length > 0);
    Document doc = searcher.doc(topDocs.scoreDocs[0].doc);
    assertNotNull(doc);
    assertEquals(String.valueOf(lastSubId), doc.get("subId"));
    searcher.close();
  }
Exemplo n.º 7
0
 private String convertNumber(Number number) {
   if (Integer.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (Double.class.isInstance(number)) {
     return NumericUtils.doubleToPrefixCoded(number.doubleValue());
   } else if (Long.class.isInstance(number)) {
     return NumericUtils.longToPrefixCoded(number.longValue());
   } else if (Float.class.isInstance(number)) {
     return NumericUtils.floatToPrefixCoded(number.floatValue());
   } else if (Byte.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (Short.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (BigDecimal.class.isInstance(number)) {
     return NumericUtils.doubleToPrefixCoded(number.doubleValue());
   } else if (BigInteger.class.isInstance(number)) {
     return NumericUtils.longToPrefixCoded(number.longValue());
   } else {
     throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
   }
 }
  /** Test that the index can be rebuilt. */
  @Test
  public void testRebuild() throws CorruptIndexException, IOException, InterruptedException {

    // Assert no job is running
    assertFalse(indexer.isJobRunning());
    assertEquals("None", indexer.getCurrentJobLabel());
    assertEquals(-1L, indexer.getCurrentJobProgress());
    assertEquals(-1L, indexer.getCurrentJobTotal());

    // Kickoff rebuilding the index
    indexer.rebuild(false);

    // Check that a job is running
    assertTrue(indexer.isJobRunning());
    assertEquals("Rebuild Index", indexer.getCurrentJobLabel());
    assertTrue(indexer.getCurrentJobProgress() >= 0);
    assertTrue(indexer.getCurrentJobTotal() >= 100);

    // Wait for the current job to complete
    for (int i = 0; i < 1000; i++) {
      Thread.sleep(10);
      if (!indexer.isJobRunning()) break;
    }
    assertFalse(indexer.isJobRunning());

    // Check that the first submission is present in the index.
    Submission sub = subRepo.findAllSubmissions().next();
    IndexReader reader = IndexReader.open(indexer.index);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermQuery(new Term("subId", NumericUtils.longToPrefixCoded(sub.getId())));

    TopDocs topDocs = searcher.search(query, 1);
    assertNotNull(topDocs);
    assertNotNull(topDocs.scoreDocs);
    assertTrue(topDocs.scoreDocs.length > 0);
    Document doc = searcher.doc(topDocs.scoreDocs[0].doc);
    assertNotNull(doc);
    assertEquals(String.valueOf(sub.getId()), doc.get("subId"));

    searcher.close();
  }
Exemplo n.º 9
0
  public static void displayPointRanges(Scanner in) {
    double[][] point = getPoints(in, 1);
    long hash, hashUpper;
    double lon, lat, lonUpper, latUpper;

    for (int i = 63; i >= 45; i -= GeoPointField.PRECISION_STEP) {
      BytesRefBuilder brb = new BytesRefBuilder();
      NumericUtils.longToPrefixCoded(
          GeoUtils.mortonHash(point[0][LON_INDEX], point[0][LAT_INDEX]), i, brb);
      BytesRef br = brb.get();
      hash = NumericUtils.prefixCodedToLong(br);
      hashUpper = hash | ((1L << i) - 1);
      lon = GeoUtils.mortonUnhashLon(hash);
      lat = GeoUtils.mortonUnhashLat(hash);
      lonUpper = GeoUtils.mortonUnhashLon(hashUpper);
      latUpper = GeoUtils.mortonUnhashLat(hashUpper);
      System.out.println(
          i + ": " + br + " " + hash + " (" + lon + "," + lat + ")" + " : " + "(" + lonUpper + ","
              + latUpper + ")");
    }
  }
Exemplo n.º 10
0
 @Override
 public BytesRef indexedValueForSearch(Object value) {
   BytesRefBuilder bytesRef = new BytesRefBuilder();
   NumericUtils.longToPrefixCoded(parseValue(value), 0, bytesRef); // 0 because of exact match
   return bytesRef.get();
 }
Exemplo n.º 11
0
 @Override
 public BytesRef indexedValueForSearch(Object value) {
   BytesRef bytesRef = new BytesRef();
   NumericUtils.longToPrefixCoded(parseValue(value), precisionStep(), bytesRef);
   return bytesRef;
 }
Exemplo n.º 12
0
/**
 * Tests for LuceneSerializer
 *
 * @author vema
 */
public class LuceneSerializerTest {
  private LuceneSerializer serializer;
  private PathBuilder<Object> entityPath;
  private StringPath title;
  private StringPath author;
  private StringPath text;
  private StringPath rating;
  private StringPath publisher;
  private NumberPath<Integer> year;
  private NumberPath<Double> gross;

  private NumberPath<Long> longField;
  private NumberPath<Short> shortField;
  private NumberPath<Byte> byteField;
  private NumberPath<Float> floatField;

  private static final String YEAR_PREFIX_CODED = NumericUtils.intToPrefixCoded(1990);
  private static final String GROSS_PREFIX_CODED = NumericUtils.doubleToPrefixCoded(900.00);
  private static final String LONG_PREFIX_CODED = NumericUtils.longToPrefixCoded(1);
  private static final String SHORT_PREFIX_CODED = NumericUtils.intToPrefixCoded(1);
  private static final String BYTE_PREFIX_CODED = NumericUtils.intToPrefixCoded(1);
  private static final String FLOAT_PREFIX_CODED = NumericUtils.floatToPrefixCoded((float) 1.0);

  private IndexWriterConfig config;
  private RAMDirectory idx;
  private IndexWriter writer;
  private IndexSearcher searcher;

  private final QueryMetadata metadata = new DefaultQueryMetadata();

  private Document createDocument() {
    Document doc = new Document();

    doc.add(new Field("title", new StringReader("Jurassic Park")));
    doc.add(new Field("author", new StringReader("Michael Crichton")));
    doc.add(new Field("text", new StringReader("It's a UNIX system! I know this!")));
    doc.add(new Field("rating", new StringReader("Good")));
    doc.add(new Field("publisher", "", Store.YES, Index.ANALYZED));
    doc.add(new NumericField("year", Store.YES, true).setIntValue(1990));
    doc.add(new NumericField("gross", Store.YES, true).setDoubleValue(900.00));

    doc.add(new NumericField("longField", Store.YES, true).setLongValue(1));
    doc.add(new NumericField("shortField", Store.YES, true).setIntValue(1));
    doc.add(new NumericField("byteField", Store.YES, true).setIntValue(1));
    doc.add(new NumericField("floatField", Store.YES, true).setFloatValue(1));

    return doc;
  }

  @Before
  public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);

    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);

    idx = new RAMDirectory();
    config =
        new IndexWriterConfig(Version.LUCENE_31, new StandardAnalyzer(Version.LUCENE_30))
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument());

    writer.close();

    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
  }

  @After
  public void tearDown() throws Exception {
    searcher.close();
  }

  private void testQuery(Expression<?> expr, int expectedHits) throws Exception {
    Query query = serializer.toQuery(expr, metadata);
    TopDocs docs = searcher.search(query, 100);
    assertEquals(expectedHits, docs.totalHits);
  }

  private void testQuery(Expression<?> expr, String expectedQuery, int expectedHits)
      throws Exception {
    Query query = serializer.toQuery(expr, metadata);
    TopDocs docs = searcher.search(query, 100);
    assertEquals(expectedHits, docs.totalHits);
    assertEquals(expectedQuery, query.toString());
  }

  @Test
  public void QueryElement() throws Exception {
    Query query1 = serializer.toQuery(author.like("Michael"), metadata);
    Query query2 = serializer.toQuery(text.like("Text"), metadata);

    BooleanExpression query =
        BooleanExpression.anyOf(new QueryElement(query1), new QueryElement(query2));
    testQuery(query, "author:michael text:text", 1);
  }

  @Test
  public void Like() throws Exception {
    testQuery(author.like("*ichael*"), "author:*ichael*", 1);
  }

  @Test
  public void Like_Custom_Wildcard_Single_Character() throws Exception {
    testQuery(author.like("Mi?hael"), "author:mi?hael", 1);
  }

  @Test
  public void Like_Custom_Wildcard_Multiple_Character() throws Exception {
    testQuery(text.like("*U*X*"), "text:*u*x*", 1);
  }

  @Test
  public void Like_Phrase() throws Exception {
    testQuery(title.like("*rassic Par*"), "+title:**rassic* +title:*par**", 1);
  }

  @Test
  public void Like_or_like() throws Exception {
    testQuery(title.like("House").or(author.like("*ichae*")), "title:house author:*ichae*", 1);
  }

  @Test
  public void Like_and_like() throws Exception {
    testQuery(title.like("*assic*").and(rating.like("G?od")), "+title:*assic* +rating:g?od", 1);
  }

  @Test
  public void Eq() throws Exception {
    testQuery(rating.eq("good"), "rating:good", 1);
  }

  @Test
  public void Eq_with_deep_path() throws Exception {
    StringPath deepPath = entityPath.get("property1", Object.class).getString("property2");
    testQuery(deepPath.eq("good"), "property1.property2:good", 0);
  }

  @Test
  public void FuzzyLike() throws Exception {
    testQuery(LuceneExpressions.fuzzyLike(rating, "Good"), "rating:Good~0.5", 1);
  }

  @Test
  public void FuzzyLike_with_Similarity() throws Exception {
    testQuery(LuceneExpressions.fuzzyLike(rating, "Good", 0.6f), "rating:Good~0.6", 1);
  }

  @Test
  public void FuzzyLike_with_Similarity_and_prefix() throws Exception {
    testQuery(LuceneExpressions.fuzzyLike(rating, "Good", 0.6f, 0), "rating:Good~0.6", 1);
  }

  @Test
  public void Eq_Numeric_Integer() throws Exception {
    testQuery(year.eq(1990), "year:" + YEAR_PREFIX_CODED, 1);
  }

  @Test
  public void Eq_Numeric_Double() throws Exception {
    testQuery(gross.eq(900.00), "gross:" + GROSS_PREFIX_CODED, 1);
  }

  @Test
  public void Eq_Numeric() throws Exception {
    testQuery(longField.eq(1l), "longField:" + LONG_PREFIX_CODED, 1);
    testQuery(shortField.eq((short) 1), "shortField:" + SHORT_PREFIX_CODED, 1);
    testQuery(byteField.eq((byte) 1), "byteField:" + BYTE_PREFIX_CODED, 1);
    testQuery(floatField.eq((float) 1.0), "floatField:" + FLOAT_PREFIX_CODED, 1);
  }

  @Test
  public void Equals_Ignores_Case() throws Exception {
    testQuery(title.eq("Jurassic"), "title:jurassic", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Title_Equals_Ignore_Case_Or_Year_Equals() throws Exception {
    testQuery(
        title.equalsIgnoreCase("House").or(year.eq(1990)),
        "title:house year:" + YEAR_PREFIX_CODED,
        1);
  }

  @Test
  public void Eq_and_eq() throws Exception {
    testQuery(
        title.eq("Jurassic Park").and(year.eq(1990)),
        "+title:\"jurassic park\" +year:" + YEAR_PREFIX_CODED,
        1);
  }

  @Test
  public void Eq_and_Eq_and_eq() throws Exception {
    testQuery(
        title.eq("Jurassic Park").and(year.eq(1990)).and(author.eq("Michael Crichton")),
        "+(+title:\"jurassic park\" +year:" + YEAR_PREFIX_CODED + ") +author:\"michael crichton\"",
        1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Equals_Ignore_Case_And_Or() throws Exception {
    testQuery(
        title
            .equalsIgnoreCase("Jurassic Park")
            .and(rating.equalsIgnoreCase("Bad"))
            .or(author.equalsIgnoreCase("Michael Crichton")),
        "(+title:\"jurassic park\" +rating:bad) author:\"michael crichton\"",
        1);
  }

  @Test
  public void Eq_or_Eq_and_Eq_Does_Not_Find_Results() throws Exception {
    testQuery(
        title.eq("jeeves").or(rating.eq("superb")).and(author.eq("michael crichton")),
        "+(title:jeeves rating:superb) +author:\"michael crichton\"",
        0);
  }

  @Test
  public void Eq_Phrase() throws Exception {
    testQuery(title.eq("Jurassic Park"), "title:\"jurassic park\"", 1);
  }

  @Test
  @Ignore("Not easily done in Lucene!")
  public void Publisher_Equals_Empty_String() throws Exception {
    testQuery(publisher.eq(""), "publisher:", 1);
  }

  @Test
  public void Eq_Phrase_Should_Not_Find_Results_But_LuceNe_Semantics_Differs_From_Querydsls()
      throws Exception {
    testQuery(text.eq("UNIX System"), "text:\"unix system\"", 1);
  }

  @Test
  public void Eq_Phrase_Does_Not_Find_Results_Because_Word_In_Middle() throws Exception {
    testQuery(title.eq("Jurassic Amusement Park"), "title:\"jurassic amusement park\"", 0);
  }

  @Test
  public void Like_not_Does_Not_Find_Results() throws Exception {
    testQuery(title.like("*H*e*").not(), "-title:*h*e* +*:*", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Title_Equals_Ignore_Case_Negation_Or_Rating_Equals_Ignore_Case() throws Exception {
    testQuery(
        title.equalsIgnoreCase("House").not().or(rating.equalsIgnoreCase("Good")),
        "-title:house rating:good",
        1);
  }

  @Test
  public void Eq_not_Does_Not_Find_Results() throws Exception {
    testQuery(title.eq("Jurassic Park").not(), "-title:\"jurassic park\" +*:*", 0);
  }

  @Test
  public void Title_Equals_Not_House() throws Exception {
    testQuery(title.eq("house").not(), "-title:house +*:*", 1);
  }

  @Test
  public void Eq_and_Eq_not_Does_Not_Find_Results_Because_Second_Expression_Finds_Nothing()
      throws Exception {
    testQuery(
        rating.eq("superb").and(title.eq("house").not()), "+rating:superb +(-title:house +*:*)", 0);
  }

  @Test
  public void Not_Equals_Finds_One() throws Exception {
    testQuery(title.ne("house"), "-title:house +*:*", 1);
  }

  @Test
  public void Not_Equals_Finds_None() throws Exception {
    testQuery(title.ne("Jurassic Park"), "-title:\"jurassic park\" +*:*", 0);
  }

  @Test
  public void Nothing_Found_With_Not_Equals_Or_Equals() throws Exception {
    testQuery(
        title.ne("jurassic park").or(rating.eq("lousy")),
        "(-title:\"jurassic park\" +*:*) rating:lousy",
        0);
  }

  @Test
  public void Ne_and_eq() throws Exception {
    testQuery(title.ne("house").and(rating.eq("good")), "+(-title:house +*:*) +rating:good", 1);
  }

  @Test
  public void StartsWith() throws Exception {
    testQuery(title.startsWith("Jurassi"), "title:jurassi*", 1);
  }

  @Test
  public void StartsWith_Phrase() throws Exception {
    testQuery(title.startsWith("jurassic par"), "+title:jurassic* +title:*par*", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Starts_With_Ignore_Case_Phrase_Does_Not_Find_Results() throws Exception {
    testQuery(title.startsWithIgnoreCase("urassic Par"), "+title:urassic* +title:*par*", 0);
  }

  @Test
  public void EndsWith() throws Exception {
    testQuery(title.endsWith("ark"), "title:*ark", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Ends_With_Ignore_Case_Phrase() throws Exception {
    testQuery(title.endsWithIgnoreCase("sic Park"), "+title:*sic* +title:*park", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Ends_With_Ignore_Case_Phrase_Does_Not_Find_Results() throws Exception {
    testQuery(title.endsWithIgnoreCase("sic Par"), "+title:*sic* +title:*par", 0);
  }

  @Test
  public void Contains() throws Exception {
    testQuery(title.contains("rassi"), "title:*rassi*", 1);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void Contains_Ignore_Case_Phrase() throws Exception {
    testQuery(title.containsIgnoreCase("rassi Pa"), "+title:*rassi* +title:*pa*", 1);
  }

  @Test
  public void Contains_User_Inputted_Wildcards_Dont_Work() throws Exception {
    testQuery(title.contains("r*i"), "title:*r\\*i*", 0);
  }

  @Test
  public void Between() throws Exception {
    testQuery(title.between("Indiana", "Kundun"), "title:[indiana TO kundun]", 1);
  }

  @Test
  public void Between_Numeric_Integer() throws Exception {
    testQuery(year.between(1980, 2000), "year:[1980 TO 2000]", 1);
  }

  @Test
  public void Between_Numeric_Double() throws Exception {
    testQuery(gross.between(10.00, 19030.00), "gross:[10.0 TO 19030.0]", 1);
  }

  @Test
  public void Between_Numeric() throws Exception {
    testQuery(longField.between(0l, 2l), "longField:[0 TO 2]", 1);
    testQuery(shortField.between((short) 0, (short) 2), "shortField:[0 TO 2]", 1);
    testQuery(byteField.between((byte) 0, (byte) 2), "byteField:[0 TO 2]", 1);
    testQuery(floatField.between((float) 0.0, (float) 2.0), "floatField:[0.0 TO 2.0]", 1);
  }

  @Test
  public void Between_Is_Inclusive_From_Start() throws Exception {
    testQuery(title.between("Jurassic", "Kundun"), "title:[jurassic TO kundun]", 1);
  }

  @Test
  public void Between_Is_Inclusive_To_End() throws Exception {
    testQuery(title.between("Indiana", "Jurassic"), "title:[indiana TO jurassic]", 1);
  }

  @Test
  public void Between_Does_Not_Find_Results() throws Exception {
    testQuery(title.between("Indiana", "Jurassib"), "title:[indiana TO jurassib]", 0);
  }

  @Test
  public void In() throws Exception {
    testQuery(title.in(Arrays.asList("jurassic", "park")), "title:jurassic title:park", 1);
    testQuery(title.in("jurassic", "park"), "title:jurassic title:park", 1);
    testQuery(title.eq("jurassic").or(title.eq("park")), "title:jurassic title:park", 1);
  }

  @Test
  public void Lt() throws Exception {
    testQuery(rating.lt("Superb"), "rating:{* TO superb}", 1);
  }

  @Test
  public void Lt_Numeric_Integer() throws Exception {
    testQuery(year.lt(1991), "year:{* TO 1991}", 1);
  }

  @Test
  public void Lt_Numeric_Double() throws Exception {
    testQuery(gross.lt(10000.0), "gross:{* TO 10000.0}", 1);
  }

  @Test
  public void Lt_Not_In_Range_Because_Equal() throws Exception {
    testQuery(rating.lt("Good"), "rating:{* TO good}", 0);
  }

  @Test
  public void Lt_Numeric_Integer_Not_In_Range_Because_Equal() throws Exception {
    testQuery(year.lt(1990), "year:{* TO 1990}", 0);
  }

  @Test
  public void Lt_Numeric_Double_Not_In_Range_Because_Equal() throws Exception {
    testQuery(gross.lt(900.0), "gross:{* TO 900.0}", 0);
  }

  @Test
  public void Loe() throws Exception {
    testQuery(rating.loe("Superb"), "rating:[* TO superb]", 1);
  }

  @Test
  public void Loe_Numeric_Integer() throws Exception {
    testQuery(year.loe(1991), "year:[* TO 1991]", 1);
  }

  @Test
  public void Loe_Numeric_Double() throws Exception {
    testQuery(gross.loe(903.0), "gross:[* TO 903.0]", 1);
  }

  @Test
  public void Loe_Equal() throws Exception {
    testQuery(rating.loe("Good"), "rating:[* TO good]", 1);
  }

  @Test
  public void Loe_Numeric_Integer_Equal() throws Exception {
    testQuery(year.loe(1990), "year:[* TO 1990]", 1);
  }

  @Test
  public void Loe_Numeric_Double_Equal() throws Exception {
    testQuery(gross.loe(900.0), "gross:[* TO 900.0]", 1);
  }

  @Test
  public void Loe_Not_Found() throws Exception {
    testQuery(rating.loe("Bad"), "rating:[* TO bad]", 0);
  }

  @Test
  public void Loe_Numeric_Integer_Not_Found() throws Exception {
    testQuery(year.loe(1989), "year:[* TO 1989]", 0);
  }

  @Test
  public void Loe_Numeric_Double_Not_Found() throws Exception {
    testQuery(gross.loe(899.9), "gross:[* TO 899.9]", 0);
  }

  @Test
  public void Gt() throws Exception {
    testQuery(rating.gt("Bad"), "rating:{bad TO *}", 1);
  }

  @Test
  public void Gt_Numeric_Integer() throws Exception {
    testQuery(year.gt(1989), "year:{1989 TO *}", 1);
  }

  @Test
  public void Gt_Numeric_Double() throws Exception {
    testQuery(gross.gt(100.00), "gross:{100.0 TO *}", 1);
  }

  @Test
  public void Gt_Not_In_Range_Because_Equal() throws Exception {
    testQuery(rating.gt("Good"), "rating:{good TO *}", 0);
  }

  @Test
  public void Gt_Numeric_Integer_Not_In_Range_Because_Equal() throws Exception {
    testQuery(year.gt(1990), "year:{1990 TO *}", 0);
  }

  @Test
  public void Gt_Numeric_Double_Not_In_Range_Because_Equal() throws Exception {
    testQuery(gross.gt(900.00), "gross:{900.0 TO *}", 0);
  }

  @Test
  public void Goe() throws Exception {
    testQuery(rating.goe("Bad"), "rating:[bad TO *]", 1);
  }

  @Test
  public void Goe_Numeric_Integer() throws Exception {
    testQuery(year.goe(1989), "year:[1989 TO *]", 1);
  }

  @Test
  public void Goe_Numeric_Double() throws Exception {
    testQuery(gross.goe(320.50), "gross:[320.5 TO *]", 1);
  }

  @Test
  public void Goe_Equal() throws Exception {
    testQuery(rating.goe("Good"), "rating:[good TO *]", 1);
  }

  @Test
  public void Goe_Numeric_Integer_Equal() throws Exception {
    testQuery(year.goe(1990), "year:[1990 TO *]", 1);
  }

  @Test
  public void Goe_Numeric_Double_Equal() throws Exception {
    testQuery(gross.goe(900.00), "gross:[900.0 TO *]", 1);
  }

  @Test
  public void Goe_Not_Found() throws Exception {
    testQuery(rating.goe("Hood"), "rating:[hood TO *]", 0);
  }

  @Test
  public void Goe_Numeric_Integer_Not_Found() throws Exception {
    testQuery(year.goe(1991), "year:[1991 TO *]", 0);
  }

  @Test
  public void Goe_Numeric_Double_Not_Found() throws Exception {
    testQuery(gross.goe(900.10), "gross:[900.1 TO *]", 0);
  }

  @Test
  public void Equals_Empty_String() throws Exception {
    testQuery(title.eq(""), "title:", 0);
  }

  @Test
  public void Not_Equals_Empty_String() throws Exception {
    testQuery(title.ne(""), "-title: +*:*", 1);
  }

  @Test
  public void Contains_Empty_String() throws Exception {
    testQuery(title.contains(""), "title:**", 1);
  }

  @Test
  public void Like_Empty_String() throws Exception {
    testQuery(title.like(""), "title:", 0);
  }

  @Test
  public void Starts_With_Empty_String() throws Exception {
    testQuery(title.startsWith(""), "title:*", 1);
  }

  @Test
  public void Ends_With_Empty_String() throws Exception {
    testQuery(title.endsWith(""), "title:*", 1);
  }

  @Test
  public void Between_Empty_Strings() throws Exception {
    testQuery(title.between("", ""), "title:[ TO ]", 0);
  }

  @Test
  public void BooleanBuilder() throws Exception {
    testQuery(new BooleanBuilder(gross.goe(900.10)), "gross:[900.1 TO *]", 0);
  }

  @Test
  @Ignore
  public void Fuzzy() throws Exception {
    fail("Not yet implemented!");
  }

  @Test
  @Ignore
  public void Proximity() throws Exception {
    fail("Not yet implemented!");
  }

  @Test
  @Ignore
  public void Boost() throws Exception {
    fail("Not yet implemented!");
  }

  private boolean unsupportedOperation(Predicate filter) {
    if (filter instanceof Operation<?>) {
      Operator<?> op = ((Operation<?>) filter).getOperator();
      if (op == Ops.STARTS_WITH_IC
          || op == Ops.EQ_IGNORE_CASE
          || op == Ops.STARTS_WITH_IC
          || op == Ops.ENDS_WITH_IC
          || op == Ops.STRING_CONTAINS_IC) {
        return true;
      }
    }
    return false;
  }

  @Test
  public void various() throws Exception {
    MatchingFiltersFactory filters = new MatchingFiltersFactory(Module.LUCENE, Target.LUCENE);
    for (Predicate filter : filters.string(title, StringConstant.create("jurassic park"))) {
      if (unsupportedOperation(filter)) {
        continue;
      }
      testQuery(filter, 1);
    }

    for (Predicate filter : filters.string(author, StringConstant.create("michael crichton"))) {
      if (unsupportedOperation(filter)) {
        continue;
      }
      testQuery(filter, 1);
    }

    for (Predicate filter : filters.string(title, StringConstant.create("1990"))) {
      if (unsupportedOperation(filter)) {
        continue;
      }
      testQuery(filter, 0);
    }
  }
}
Exemplo n.º 13
0
 private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
   final Random rnd = newRandom();
   String field = "field" + precisionStep;
   int termCountT = 0, termCountC = 0;
   for (int i = 0; i < 50; i++) {
     long lower = (long) (rnd.nextDouble() * noDocs * distance) + startOffset;
     long upper = (long) (rnd.nextDouble() * noDocs * distance) + startOffset;
     if (lower > upper) {
       long a = lower;
       lower = upper;
       upper = a;
     }
     // test inclusive range
     NumericRangeQuery<Long> tq =
         NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
     TermRangeQuery cq =
         new TermRangeQuery(
             field,
             NumericUtils.longToPrefixCoded(lower),
             NumericUtils.longToPrefixCoded(upper),
             true,
             true);
     TopDocs tTopDocs = searcher.search(tq, 1);
     TopDocs cTopDocs = searcher.search(cq, 1);
     assertEquals(
         "Returned count for NumericRangeQuery and TermRangeQuery must be equal",
         cTopDocs.totalHits,
         tTopDocs.totalHits);
     termCountT += tq.getTotalNumberOfTerms();
     termCountC += cq.getTotalNumberOfTerms();
     // test exclusive range
     tq = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false);
     cq =
         new TermRangeQuery(
             field,
             NumericUtils.longToPrefixCoded(lower),
             NumericUtils.longToPrefixCoded(upper),
             false,
             false);
     tTopDocs = searcher.search(tq, 1);
     cTopDocs = searcher.search(cq, 1);
     assertEquals(
         "Returned count for NumericRangeQuery and TermRangeQuery must be equal",
         cTopDocs.totalHits,
         tTopDocs.totalHits);
     termCountT += tq.getTotalNumberOfTerms();
     termCountC += cq.getTotalNumberOfTerms();
     // test left exclusive range
     tq = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true);
     cq =
         new TermRangeQuery(
             field,
             NumericUtils.longToPrefixCoded(lower),
             NumericUtils.longToPrefixCoded(upper),
             false,
             true);
     tTopDocs = searcher.search(tq, 1);
     cTopDocs = searcher.search(cq, 1);
     assertEquals(
         "Returned count for NumericRangeQuery and TermRangeQuery must be equal",
         cTopDocs.totalHits,
         tTopDocs.totalHits);
     termCountT += tq.getTotalNumberOfTerms();
     termCountC += cq.getTotalNumberOfTerms();
     // test right exclusive range
     tq = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false);
     cq =
         new TermRangeQuery(
             field,
             NumericUtils.longToPrefixCoded(lower),
             NumericUtils.longToPrefixCoded(upper),
             true,
             false);
     tTopDocs = searcher.search(tq, 1);
     cTopDocs = searcher.search(cq, 1);
     assertEquals(
         "Returned count for NumericRangeQuery and TermRangeQuery must be equal",
         cTopDocs.totalHits,
         tTopDocs.totalHits);
     termCountT += tq.getTotalNumberOfTerms();
     termCountC += cq.getTotalNumberOfTerms();
   }
   if (precisionStep == Integer.MAX_VALUE) {
     assertEquals(
         "Total number of terms should be equal for unlimited precStep", termCountT, termCountC);
   } else {
     System.out.println("Average number of terms during random search on '" + field + "':");
     System.out.println(" Trie query: " + (((double) termCountT) / (50 * 4)));
     System.out.println(" Classical query: " + (((double) termCountC) / (50 * 4)));
   }
 }
  /**
   * This method produces the common part of the query handle the filter search clauses.
   *
   * @param andQuery The existing and-based query
   * @param filter The filter search paramaters.
   * @param submissions Whether this is for submissions or action logs
   */
  public void buildQuery(BooleanQuery andQuery, SearchFilter filter, boolean submissions) {
    QueryParser parser = new QueryParser(indexer.version, "searchText", indexer.standardAnalyzer);

    // Include Submission filter
    if (filter.getIncludedSubmissions().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (Submission sub : filter.getIncludedSubmissions()) {
        orQuery.add(
            new TermQuery(new Term("subId", NumericUtils.longToPrefixCoded(sub.getId()))),
            Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Include Log filter
    if (filter.getIncludedActionLogs().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (ActionLog log : filter.getIncludedActionLogs()) {
        orQuery.add(
            new TermQuery(new Term("logId", NumericUtils.longToPrefixCoded(log.getId()))),
            Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Exclude Submission filter
    for (Submission sub : filter.getExcludedSubmissions()) {
      andQuery.add(
          new TermQuery(new Term("subId", NumericUtils.longToPrefixCoded(sub.getId()))),
          Occur.MUST_NOT);
    }

    // Exclude Log filter
    for (ActionLog log : filter.getExcludedActionLogs()) {
      andQuery.add(
          new TermQuery(new Term("logId", NumericUtils.longToPrefixCoded(log.getId()))),
          Occur.MUST_NOT);
    }

    // Search Text Filter
    if (filter.getSearchText().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String searchText : filter.getSearchText()) {
        try {
          // First try to interpret it as a complex lucene search string.
          orQuery.add(parser.parse(searchText), Occur.SHOULD);
        } catch (ParseException e) {
          // If that fails just fall back to a term query.
          orQuery.add(new TermQuery(new Term("searchText", searchText)), Occur.SHOULD);
        }
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // State Filter
    if (filter.getStates().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String stateName : filter.getStates()) {
        State state = stateManager.getState(stateName);
        orQuery.add(new TermQuery(new Term("state", state.getDisplayName())), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Assignee Filter
    if (filter.getAssignees().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (Person assignee : filter.getAssignees()) {

        long assigneeId = 0;
        if (assignee != null) assigneeId = assignee.getId();

        orQuery.add(
            new TermQuery(new Term("searchAssigned", NumericUtils.longToPrefixCoded(assigneeId))),
            Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Embargo Filter
    if (filter.getEmbargoTypes().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (EmbargoType embargo : filter.getEmbargoTypes()) {
        orQuery.add(
            new TermQuery(
                new Term("embargo", embargo.getName() + " " + embargo.getGuarantor().name())),
            Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Graduation Semester Filter
    if (filter.getGraduationSemesters().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (Semester semester : filter.getGraduationSemesters()) {

        // We can't index it if it dosn't have a date.
        if (semester.year == null) continue;

        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, semester.year);
        if (semester.month != null) {
          cal.set(Calendar.MONTH, semester.month);
        }
        orQuery.add(
            new TermQuery(
                new Term(
                    "graduationSemester", NumericUtils.longToPrefixCoded(cal.getTimeInMillis()))),
            Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Degree Filter
    if (filter.getDegrees().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String degree : filter.getDegrees()) {
        orQuery.add(new TermQuery(new Term("degree", degree)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Department Filter
    if (filter.getDepartments().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String dept : filter.getDepartments()) {
        orQuery.add(new TermQuery(new Term("department", dept)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Program Filter
    if (filter.getPrograms().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String program : filter.getPrograms()) {
        orQuery.add(new TermQuery(new Term("program", program)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // College Filter
    if (filter.getColleges().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String college : filter.getColleges()) {
        orQuery.add(new TermQuery(new Term("college", college)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Major Filter
    if (filter.getMajors().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String major : filter.getMajors()) {
        orQuery.add(new TermQuery(new Term("major", major)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // Document Type Filter
    if (filter.getDocumentTypes().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (String docType : filter.getDocumentTypes()) {
        orQuery.add(new TermQuery(new Term("documentType", docType)), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }

    // UMI Release Filter
    if (filter.getUMIRelease() != null) {
      if (filter.getUMIRelease()) {
        andQuery.add(new TermQuery(new Term("umiRelease", "yes")), Occur.MUST);
      } else {
        andQuery.add(new TermQuery(new Term("umiRelease", "no")), Occur.MUST);
      }
    }

    // Date Range Filter
    if (filter.getDateRangeStart() != null || filter.getDateRangeEnd() != null) {

      long startTime = 0;
      if (filter.getDateRangeStart() != null) startTime = filter.getDateRangeStart().getTime();

      long endTime = Long.MAX_VALUE;
      if (filter.getDateRangeEnd() != null) endTime = filter.getDateRangeEnd().getTime();

      if (submissions)
        andQuery.add(
            NumericRangeQuery.newLongRange("submissionDate", startTime, endTime, true, true),
            Occur.MUST);
      else
        andQuery.add(
            NumericRangeQuery.newLongRange("lastEventTime", startTime, endTime, true, true),
            Occur.MUST);
    }
    // Custom Action Filter
    if (filter.getCustomActions().size() > 0) {
      BooleanQuery orQuery = new BooleanQuery();
      for (CustomActionDefinition customAction : filter.getCustomActions()) {
        orQuery.add(new TermQuery(new Term("customAction", customAction.getLabel())), Occur.SHOULD);
      }
      andQuery.add(orQuery, Occur.MUST);
    }
  }
Exemplo n.º 15
0
 @Override
 public String storedToIndexed(Fieldable f) {
   if (f instanceof NumericField) {
     final Number val = ((NumericField) f).getNumericValue();
     if (val == null)
       throw new SolrException(
           SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name());
     switch (type) {
       case INTEGER:
         return NumericUtils.intToPrefixCoded(val.intValue());
       case FLOAT:
         return NumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(val.floatValue()));
       case LONG: // fallthrough!
       case DATE:
         return NumericUtils.longToPrefixCoded(val.longValue());
       case DOUBLE:
         return NumericUtils.longToPrefixCoded(
             NumericUtils.doubleToSortableLong(val.doubleValue()));
       default:
         throw new SolrException(
             SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
     }
   } else {
     // the following code is "deprecated" and only to support pre-3.2 indexes using the old
     // BinaryField encoding:
     final byte[] arr = f.getBinaryValue();
     if (arr == null)
       throw new SolrException(
           SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name());
     switch (type) {
       case INTEGER:
         return NumericUtils.intToPrefixCoded(toInt(arr));
       case FLOAT:
         {
           // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils!
           // copied from NumericUtils to not convert to/from float two times
           // code in next 2 lines is identical to: int v =
           // NumericUtils.floatToSortableInt(Float.intBitsToFloat(toInt(arr)));
           int v = toInt(arr);
           if (v < 0) v ^= 0x7fffffff;
           return NumericUtils.intToPrefixCoded(v);
         }
       case LONG: // fallthrough!
       case DATE:
         return NumericUtils.longToPrefixCoded(toLong(arr));
       case DOUBLE:
         {
           // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils!
           // copied from NumericUtils to not convert to/from double two times
           // code in next 2 lines is identical to: long v =
           // NumericUtils.doubleToSortableLong(Double.longBitsToDouble(toLong(arr)));
           long v = toLong(arr);
           if (v < 0) v ^= 0x7fffffffffffffffL;
           return NumericUtils.longToPrefixCoded(v);
         }
       default:
         throw new SolrException(
             SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
     }
   }
 }