Search in sources :

Example 6 with LindenFieldSchema

use of com.xiaomi.linden.thrift.common.LindenFieldSchema in project linden by XiaoMi.

the class LindenQueryParser method getRangeQuery.

@Override
protected Query getRangeQuery(String field, String min, String max, boolean startInclusive, boolean endInclusive) {
    LindenFieldSchema fieldSchema = config.getFieldSchema(field);
    String name = fieldSchema.getName();
    LindenType indexFieldType = fieldSchema.getType();
    Query query = null;
    switch(indexFieldType) {
        case STRING:
            query = new TermRangeQuery(name, bytesRefVal(min), bytesRefVal(max), startInclusive, endInclusive);
            break;
        case INTEGER:
            query = NumericRangeQuery.newIntRange(name, intVal(min), intVal(max), startInclusive, endInclusive);
            break;
        case LONG:
            query = NumericRangeQuery.newLongRange(name, longVal(min), longVal(max), startInclusive, endInclusive);
            break;
        case DOUBLE:
            query = NumericRangeQuery.newDoubleRange(name, doubleVal(min), doubleVal(max), startInclusive, endInclusive);
            break;
        case FLOAT:
            query = NumericRangeQuery.newFloatRange(name, floatVal(min), floatVal(max), startInclusive, endInclusive);
            break;
        default:
            break;
    }
    return query;
}
Also used : LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) LindenType(com.xiaomi.linden.thrift.common.LindenType)

Example 7 with LindenFieldSchema

use of com.xiaomi.linden.thrift.common.LindenFieldSchema in project linden by XiaoMi.

the class TestLindenJiebaAnalyzerSearchMode method init.

@Override
public void init() {
    lindenConfig.setIndexType(LindenConfig.IndexType.RAM);
    lindenConfig.setClusterUrl("127.0.0.1:2181/mock");
    LindenSchema schema = new LindenSchema().setId("id");
    schema.addToFields(new LindenFieldSchema().setName("title").setIndexed(true).setTokenized(true).setSnippet(true).setMulti(true));
    lindenConfig.setSchema(schema);
    lindenConfig.putToProperties("search.analyzer.class", "com.xiaomi.linden.lucene.analyzer.LindenJiebaAnalyzerFactory");
    lindenConfig.putToProperties("index.analyzer.class", "com.xiaomi.linden.lucene.analyzer.LindenJiebaAnalyzerFactory");
    lindenConfig.putToProperties("index.analyzer.mode", "search");
}
Also used : LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) LindenSchema(com.xiaomi.linden.thrift.common.LindenSchema)

Example 8 with LindenFieldSchema

use of com.xiaomi.linden.thrift.common.LindenFieldSchema in project linden by XiaoMi.

the class TestLindenMMSeg4jAnalyzer method init.

@Override
public void init() {
    lindenConfig.setIndexType(LindenConfig.IndexType.RAM);
    lindenConfig.setClusterUrl("127.0.0.1:2181/mock");
    LindenSchema schema = new LindenSchema().setId("id");
    schema.addToFields(new LindenFieldSchema().setName("title").setIndexed(true).setTokenized(true).setSnippet(true).setMulti(true));
    lindenConfig.setSchema(schema);
    lindenConfig.putToProperties("search.analyzer.class", "com.xiaomi.linden.lucene.analyzer.LindenMMSeg4jAnalyzerFactory");
    lindenConfig.putToProperties("index.analyzer.class", "com.xiaomi.linden.lucene.analyzer.LindenMMSeg4jAnalyzerFactory");
}
Also used : LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) LindenSchema(com.xiaomi.linden.thrift.common.LindenSchema)

Example 9 with LindenFieldSchema

use of com.xiaomi.linden.thrift.common.LindenFieldSchema in project linden by XiaoMi.

the class TestGlobalIDF method init.

@Override
public void init() throws Exception {
    LindenSchema schema = new LindenSchema().setId("id");
    schema.addToFields(new LindenFieldSchema().setName("title").setIndexed(true).setStored(true).setTokenized(true).setOmitNorms(true));
    schema.addToFields(new LindenFieldSchema().setName("field1").setIndexed(true).setStored(true).setTokenized(true).setOmitNorms(true));
    schema.addToFields(new LindenFieldSchema().setName("field2").setIndexed(true).setStored(true).setTokenized(true).setOmitNorms(true));
    schema.addToFields(new LindenFieldSchema().setName("rank").setType(LindenType.FLOAT).setIndexed(true).setStored(true));
    schema.addToFields(new LindenFieldSchema().setName("cat1").setType(LindenType.INTEGER).setIndexed(true).setStored(true));
    schema.addToFields(new LindenFieldSchema().setName("cat2").setType(LindenType.DOUBLE).setIndexed(true).setStored(true));
    schema.addToFields(new LindenFieldSchema().setName("tagstr").setIndexed(true));
    schema.addToFields(new LindenFieldSchema().setName("tagnum").setType(LindenType.INTEGER).setIndexed(true));
    lindenConfig.setSchema(schema);
}
Also used : LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) LindenSchema(com.xiaomi.linden.thrift.common.LindenSchema)

Example 10 with LindenFieldSchema

use of com.xiaomi.linden.thrift.common.LindenFieldSchema in project linden by XiaoMi.

the class LindenDocParser method parse.

public static Document parse(LindenDocument lindenDoc, LindenConfig config) {
    if (!lindenDoc.isSetFields()) {
        return null;
    }
    Document doc = new Document();
    doc.add(new StringField(config.getSchema().getId(), lindenDoc.getId(), Field.Store.YES));
    for (LindenField field : lindenDoc.getFields()) {
        LindenFieldSchema schema = field.getSchema();
        Field.Store isStored = schema.isStored() ? Field.Store.YES : Field.Store.NO;
        String name = field.getSchema().getName();
        Object value;
        if (!schema.isIndexed() && schema.isStored()) {
            doc.add(new Field(name, field.getValue(), STORED_ONLY));
        }
        switch(schema.getType()) {
            case INTEGER:
                value = Integer.valueOf(field.getValue());
                if (schema.isIndexed()) {
                    doc.add(new IntField(name, (Integer) value, isStored));
                }
                if (schema.isDocValues()) {
                    long docValuesBits = ((Integer) value).longValue();
                    doc.add(new NumericDocValuesField(name, docValuesBits));
                }
                break;
            case LONG:
                value = Long.valueOf(field.getValue());
                if (schema.isIndexed()) {
                    doc.add(new LongField(name, (Long) value, isStored));
                }
                if (schema.isDocValues()) {
                    doc.add(new NumericDocValuesField(name, (long) value));
                }
                break;
            case DOUBLE:
                value = Double.valueOf(field.getValue());
                if (schema.isIndexed()) {
                    doc.add(new DoubleField(name, (Double) value, isStored));
                }
                if (schema.isDocValues()) {
                    long docValuesBits = Double.doubleToLongBits((Double) value);
                    doc.add(new NumericDocValuesField(name, docValuesBits));
                }
                break;
            case FLOAT:
                value = Float.valueOf(field.getValue());
                if (schema.isIndexed()) {
                    doc.add(new FloatField(name, (Float) value, isStored));
                }
                if (schema.isDocValues()) {
                    long docValuesBits = Float.floatToIntBits((Float) value);
                    doc.add(new NumericDocValuesField(name, docValuesBits));
                }
                break;
            case STRING:
                if (Strings.isNullOrEmpty(field.getValue())) {
                    break;
                }
                if (schema.isIndexed()) {
                    FieldType type = new FieldType();
                    type.setTokenized(schema.isTokenized());
                    type.setIndexed(schema.isIndexed());
                    type.setStored(schema.isStored());
                    type.setOmitNorms(schema.isOmitNorms());
                    if (schema.isSnippet()) {
                        type.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
                        // snippet will use the stored info.
                        type.setStored(true);
                    }
                    if (schema.isOmitFreqs()) {
                        type.setIndexOptions(FieldInfo.IndexOptions.DOCS_ONLY);
                    }
                    doc.add(new Field(name, field.getValue(), type));
                }
                if (schema.isDocValues()) {
                    BytesRef bytes = new BytesRef(field.getValue());
                    doc.add(new BinaryDocValuesField(name, bytes));
                }
                break;
            case FACET:
                String[] facetPath = field.getValue().split("/");
                doc.add(new FacetField(name, facetPath));
                if (schema.isIndexed()) {
                    doc.add(new StringField(name, field.getValue(), isStored));
                }
                if (schema.isDocValues()) {
                    doc.add(new BinaryDocValuesField(name, new BytesRef(field.getValue())));
                }
                break;
            default:
        }
    }
    if (lindenDoc.isSetCoordinate()) {
        Coordinate coord = lindenDoc.getCoordinate();
        Shape shape = SpatialContext.GEO.makePoint(coord.getLongitude(), coord.getLatitude());
        for (IndexableField field : config.getSpatialStrategy().createIndexableFields(shape)) {
            doc.add(field);
        }
    }
    return doc;
}
Also used : Shape(com.spatial4j.core.shape.Shape) FacetField(org.apache.lucene.facet.FacetField) IntField(org.apache.lucene.document.IntField) LindenDocument(com.xiaomi.linden.thrift.common.LindenDocument) Document(org.apache.lucene.document.Document) FloatField(org.apache.lucene.document.FloatField) LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) FacetField(org.apache.lucene.facet.FacetField) LongField(org.apache.lucene.document.LongField) StringField(org.apache.lucene.document.StringField) IndexableField(org.apache.lucene.index.IndexableField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) DoubleField(org.apache.lucene.document.DoubleField) Field(org.apache.lucene.document.Field) LindenField(com.xiaomi.linden.thrift.common.LindenField) IntField(org.apache.lucene.document.IntField) FloatField(org.apache.lucene.document.FloatField) LongField(org.apache.lucene.document.LongField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) DoubleField(org.apache.lucene.document.DoubleField) BytesRef(org.apache.lucene.util.BytesRef) LindenField(com.xiaomi.linden.thrift.common.LindenField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) FieldType(org.apache.lucene.document.FieldType) IndexableField(org.apache.lucene.index.IndexableField) Coordinate(com.xiaomi.linden.thrift.common.Coordinate) StringField(org.apache.lucene.document.StringField)

Aggregations

LindenFieldSchema (com.xiaomi.linden.thrift.common.LindenFieldSchema)31 LindenSchema (com.xiaomi.linden.thrift.common.LindenSchema)17 LindenField (com.xiaomi.linden.thrift.common.LindenField)4 JSONObject (com.alibaba.fastjson.JSONObject)3 LindenDocument (com.xiaomi.linden.thrift.common.LindenDocument)3 IOException (java.io.IOException)3 JSONArray (com.alibaba.fastjson.JSONArray)2 Coordinate (com.xiaomi.linden.thrift.common.Coordinate)2 LindenType (com.xiaomi.linden.thrift.common.LindenType)2 Document (org.apache.lucene.document.Document)2 IndexableField (org.apache.lucene.index.IndexableField)2 Shape (com.spatial4j.core.shape.Shape)1 LindenValue (com.xiaomi.linden.thrift.common.LindenValue)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1