Package org.apache.stanbol.entityhub.servicesapi.query

Examples of org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint


        Iterable<Representation> updatedIterable = yard.update(Arrays.asList(test1, test2, test3));
        assertNotNull(updatedIterable);

        // Perform a first similarity query that looks a lot like the first document
        FieldQuery query = yard.getQueryFactory().createFieldQuery();
        query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
        QueryResultList<Representation> results = yard.find(query);
        assertEquals(2, results.size());
        Iterator<Representation> it = results.iterator();
        Representation first = it.next();
        assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1", first.getId());
        // assertEquals(0.99, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));

        Representation second = it.next();
        assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2",
            second.getId());
        // assertEquals(0.80, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));

        // combine similarity with traditional filtering
        query = yard.getQueryFactory().createFieldQuery();
        query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
        query.setConstraint(filterfield, new TextConstraint(Arrays.asList("other")));
        results = yard.find(query);
        assertEquals(1, results.size());
        it = results.iterator();
        first = it.next();
View Full Code Here


                // TODO: log make the FieldQuery ensure that there is no more than one instead of similarity
                // constraint per query

                List<String> fields = new ArrayList<String>();
                fields.add(fieldConstraint.getKey());
                SimilarityConstraint simConstraint = (SimilarityConstraint) fieldConstraint.getValue();
                IndexValue indexValue = indexValueFactory.createIndexValue(simConstraint.getContext());
                fields.addAll(simConstraint.getAdditionalFields());
                if(!similarityConstraintPresent){
                    similarityConstraintPresent = true; //similarity constraint present
                    //add the constraint to the query
                    query.setQueryType(MLT_QUERY_TYPE);
                    query.set(MATCH_INCLUDE, false);
                    query.set(MIN_DOC_FREQ, 1);
                    query.set(MIN_TERM_FREQ, 1);
                    query.set(INTERESTING_TERMS, "details");
                    List<String> indexFields = new ArrayList<String>();
                    for(String field : fields){
                        IndexField indexField = new IndexField(Collections.singletonList(field),
                            IndexDataTypeEnum.TXT.getIndexType());
                        indexFields.addAll(fieldMapper.getFieldNames(indexField));
                    }
                    query.set(SIMILARITY_FIELDS, indexFields.toArray(new String[fields.size()]));
                    query.set(STREAM_BODY, indexValue.getValue());
                    processedFieldConstraints.put(fieldConstraint.getKey(), fieldConstraint.getValue());
                } else { //similarity constraint already present -> ignore further
                    //NOTE: users are informed about that by NOT including further
                    //      similarity constraints in the query included in the
                    //      response
                    log.warn("The parsed FieldQuery contains multiple Similarity constraints." +
                            "However only a single one can be supported per query. Because of " +
                            "this all further Similarity constraints will be ignored!");
                    log.warn("Ignore SimilarityConstraint:");
                    log.warn(" > Field      : {}",fieldConstraint.getKey());
                    log.warn(" > Context    : {}",simConstraint.getContext());
                    log.warn(" > Add Fields : {}",simConstraint.getAdditionalFields());
                }
            } else {
                IndexConstraint indexConstraint = createIndexConstraint(fieldConstraint);
                if (indexConstraint.isInvalid()) {
                    log.warn("Unable to create IndexConstraint for Constraint {} (type: {}) and Field {} (Reosens: {})",
View Full Code Here

                }
            }
        } else {
            fields = null;
        }
        return new SimilarityConstraint(context,fields);
    }
View Full Code Here

                if(!dataTypes.isEmpty()){
                    jConstraint.put("datatype", dataTypes.iterator().next().getShortName());
                }
                break;
            case similarity:
                SimilarityConstraint simConstraint = (SimilarityConstraint) constraint;
                jConstraint.put("context", simConstraint.getContext());
                if(!simConstraint.getAdditionalFields().isEmpty()){
                    jConstraint.put("addFields", new JSONArray(
                        simConstraint.getAdditionalFields()));
                }
                break;
            default:
                //unknown constraint type
                log.warn("Unsupported Constriant Type " + constraint.getType() + " (implementing class=" + constraint.getClass() + "| toString=" + constraint + ") -> skiped");
View Full Code Here

        }
        if(similarityContext.length() > 0 && !similarityFields.isEmpty()){
            //add similarity constraint
            Iterator<String> fieldIt = similarityFields.iterator();
            String field = fieldIt.next();
            SimilarityConstraint simConstraint;
            if(fieldIt.hasNext()){
                List<String> addFields = new ArrayList<String>(similarityFields.size()-1);
                while(fieldIt.hasNext()){
                    addFields.add(fieldIt.next());
                }
                simConstraint = new SimilarityConstraint(similarityContext.toString(),addFields);
            } else {
                simConstraint = new SimilarityConstraint(similarityContext.toString());
            }
            query.setConstraint(field, simConstraint);
        }
    }
View Full Code Here

        for (Entry<String,Constraint> fieldConstraint : fieldQuery) {
            if (fieldConstraint.getValue().getType() == ConstraintType.similarity) {
                // TODO: log make the FieldQuery ensure that there is no more than one instead of similarity
                // constraint per query
                List<String> fields = Arrays.asList(fieldConstraint.getKey());
                SimilarityConstraint simConstraint = (SimilarityConstraint) fieldConstraint.getValue();
                IndexValue indexValue = indexValueFactory.createIndexValue(simConstraint.getContext());
                fields.addAll(simConstraint.getAdditionalFields());
                query.setQueryType(MLT_QUERY_TYPE);
                query.set(MoreLikeThisParams.MATCH_INCLUDE, false);
                query.set(MoreLikeThisParams.MIN_DOC_FREQ, 1);
                query.set(MoreLikeThisParams.MIN_TERM_FREQ, 1);
                query.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
View Full Code Here

        Iterable<Representation> updatedIterable = yard.update(Arrays.asList(test1, test2, test3));
        assertNotNull(updatedIterable);

        // Perform a first similarity query that looks a lot like the first document
        FieldQuery query = yard.getQueryFactory().createFieldQuery();
        query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
        QueryResultList<Representation> results = yard.find(query);
        assertEquals(2, results.size());
        Iterator<Representation> it = results.iterator();
        Representation first = it.next();
        assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1", first.getId());
        // assertEquals(0.99, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));

        Representation second = it.next();
        assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2",
            second.getId());
        // assertEquals(0.80, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));

        // combine similarity with traditional filtering
        query = yard.getQueryFactory().createFieldQuery();
        query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
        query.setConstraint(filterfield, new TextConstraint(Arrays.asList("other")));
        results = yard.find(query);
        assertEquals(1, results.size());
        it = results.iterator();
        first = it.next();
View Full Code Here

TOP

Related Classes of org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.