All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.document.BinaryPoint Maven / Gradle / Ivy

There is a newer version: 9.11.1
Show newest version
/*
 * 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.
 */
package org.apache.lucene.document;

import java.util.Arrays;
import java.util.Comparator;

import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.StringHelper;

/** 
 * An indexed binary field for fast range filters.  If you also
 * need to store the value, you should add a separate {@link StoredField} instance.
 * 

* Finding all documents within an N-dimensional shape or range at search time is * efficient. Multiple values for the same field in one document * is allowed. *

* This field defines static factory methods for creating common queries: *

    *
  • {@link #newExactQuery(String, byte[])} for matching an exact 1D point. *
  • {@link #newSetQuery(String, byte[][]) newSetQuery(String, byte[]...)} for matching a set of 1D values. *
  • {@link #newRangeQuery(String, byte[], byte[])} for matching a 1D range. *
  • {@link #newRangeQuery(String, byte[][], byte[][])} for matching points/ranges in n-dimensional space. *
* @see PointValues */ public final class BinaryPoint extends Field { private static FieldType getType(byte[][] point) { if (point == null) { throw new IllegalArgumentException("point must not be null"); } if (point.length == 0) { throw new IllegalArgumentException("point must not be 0 dimensions"); } int bytesPerDim = -1; for(int i=0;i 0) { sb.append(' '); } sb.append(Integer.toHexString(value[i] & 0xFF)); } sb.append(')'); return sb.toString(); } }; } /** * Create a query matching any of the specified 1D values. This is the points equivalent of {@code TermsQuery}. * * @param field field name. must not be {@code null}. * @param values all values to match */ public static Query newSetQuery(String field, byte[]... values) { // Make sure all byte[] have the same length int bytesPerDim = -1; for(byte[] value : values) { if (bytesPerDim == -1) { bytesPerDim = value.length; } else if (value.length != bytesPerDim) { throw new IllegalArgumentException("all byte[] must be the same length, but saw " + bytesPerDim + " and " + value.length); } } if (bytesPerDim == -1) { // There are no points, and we cannot guess the bytesPerDim here, so we return an equivalent query: return new MatchNoDocsQuery("empty BinaryPoint.newSetQuery"); } // Don't unexpectedly change the user's incoming values array: byte[][] sortedValues = values.clone(); Arrays.sort(sortedValues, new Comparator() { @Override public int compare(byte[] a, byte[] b) { return StringHelper.compare(a.length, a, 0, b, 0); } }); final BytesRef encoded = new BytesRef(new byte[bytesPerDim]); return new PointInSetQuery(field, 1, bytesPerDim, new PointInSetQuery.Stream() { int upto; @Override public BytesRef next() { if (upto == sortedValues.length) { return null; } else { encoded.bytes = sortedValues[upto]; upto++; return encoded; } } }) { @Override protected String toString(byte[] value) { return new BytesRef(value).toString(); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy