Java Code Examples for org.apache.lucene.search.join.ScoreMode#Avg

The following examples show how to use org.apache.lucene.search.join.ScoreMode#Avg . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HasChildQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Query joinUtilHelper(String parentType, ParentChildIndexFieldData parentChildIndexFieldData, Similarity similarity, Query toQuery, ScoreType scoreType, Query innerQuery, int minChildren, int maxChildren) throws IOException {
    ScoreMode scoreMode;
    // TODO: move entirely over from ScoreType to org.apache.lucene.join.ScoreMode, when we drop the 1.x parent child code.
    switch (scoreType) {
        case NONE:
            scoreMode = ScoreMode.None;
            break;
        case MIN:
            scoreMode = ScoreMode.Min;
            break;
        case MAX:
            scoreMode = ScoreMode.Max;
            break;
        case SUM:
            scoreMode = ScoreMode.Total;
            break;
        case AVG:
            scoreMode = ScoreMode.Avg;
            break;
        default:
            throw new UnsupportedOperationException("score type [" + scoreType + "] not supported");
    }
    // 0 in pre 2.x p/c impl means unbounded
    if (maxChildren == 0) {
        maxChildren = Integer.MAX_VALUE;
    }
    return new LateParsingQuery(toQuery, innerQuery, minChildren, maxChildren, parentType, scoreMode, parentChildIndexFieldData, similarity);
}
 
Example 2
Source File: NestedQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();
    final ToBlockJoinQueryBuilder builder = new ToBlockJoinQueryBuilder(parseContext);

    float boost = 1.0f;
    ScoreMode scoreMode = ScoreMode.Avg;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("query".equals(currentFieldName)) {
                builder.query();
            } else if (parseContext.parseFieldMatcher().match(currentFieldName, FILTER_FIELD)) {
                builder.filter();
            } else if ("inner_hits".equals(currentFieldName)) {
                builder.setInnerHits(innerHitsQueryParserHelper.parse(parseContext));
            } else {
                throw new QueryParsingException(parseContext, "[nested] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("path".equals(currentFieldName)) {
                builder.setPath(parser.text());
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) {
                String sScoreMode = parser.text();
                if ("avg".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Avg;
                } else if ("min".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Min;
                } else if ("max".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Max;
                } else if ("total".equals(sScoreMode) || "sum".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Total;
                } else if ("none".equals(sScoreMode)) {
                    scoreMode = ScoreMode.None;
                } else {
                    throw new QueryParsingException(parseContext, "illegal score_mode for nested query [" + sScoreMode + "]");
                }
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new QueryParsingException(parseContext, "[nested] query does not support [" + currentFieldName + "]");
            }
        }
    }

    builder.setScoreMode(scoreMode);
    ToParentBlockJoinQuery joinQuery = builder.build();
    if (joinQuery != null) {
        joinQuery.setBoost(boost);
        if (queryName != null) {
            parseContext.addNamedQuery(queryName, joinQuery);
        }
    }
    return joinQuery;
}
 
Example 3
Source File: TestHierarchicalDocBuilder.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-12801") // this test fails easily under beasting
public void testThreeLevelHierarchy() throws Exception {
  int parentsNum = 3; //fixed for simplicity of test
  int childrenNum = 0;
  int grandChildrenNum = 0;
  
  final String parentType = "parent";
  final String childType = "child";
  final String grandChildType = "grand_child";

  List<String> parentIds = createDataIterator("select * from PARENT", parentType, parentType, parentsNum);
  Collections.shuffle(parentIds, random());
  final String parentId1 = parentIds.get(0);
  String parentId2 = parentIds.get(1);
  
  //parent 1 children
  int firstParentChildrenNum = 3; //fixed for simplicity of test
  String select = "select * from CHILD where parent_id='" + parentId1 + "'";
  List<String> childrenIds = createDataIterator(select, childType, "child of first parent", firstParentChildrenNum);
  List<String> firstParentChildrenIds = new ArrayList<String>(childrenIds);
  childrenNum += childrenIds.size();
  
  // grand children of first parent first child
  final String childId = childrenIds.get(0);
  String description = "grandchild of first parent, child of " + childId + " child";
  select = "select * from GRANDCHILD where parent_id='" + childId + "'";
  List<String> grandChildrenIds = createDataIterator(select, grandChildType, description, atLeast(2));
  grandChildrenNum += grandChildrenIds.size();
  
  // grand children of first parent second child
  {
    String childId2 = childrenIds.get(1);
    description = "grandchild of first parent, child of " + childId2 + " child";
    select = "select * from GRANDCHILD where parent_id='" + childId2 + "'";
  }
  final List<String> grandChildrenIds2 = createDataIterator(select, grandChildType, description, atLeast(2));
  grandChildrenNum += grandChildrenIds2.size();
  
  List<String> allGrandChildrenIds = new ArrayList<>(grandChildrenIds);
  allGrandChildrenIds.addAll(grandChildrenIds2);
      
  // third children of first parent has no grand children
  
  // parent 2 children (no grand children)   
  select = "select * from CHILD where parent_id='" + parentId2 + "'";
  childrenIds = createDataIterator(select, childType, "child of second parent", atLeast(2));
  childrenNum += childrenIds.size();
  
  // parent 3 has no children and grand children
  
  int totalDocsNum = parentsNum + childrenNum + grandChildrenNum;
  
  String resp = runFullImport(THREE_LEVEL_HIERARCHY_CONFIG);
  String xpath = "//arr[@name='documents']/lst[arr[@name='id']/str='"+parentId1+"']/"+
    "arr[@name='_childDocuments_']/lst[arr[@name='id']/str='"+childId+"']/"+
    "arr[@name='_childDocuments_']/lst[arr[@name='id']/str='"+grandChildrenIds.get(0)+"']";
  String results = TestHarness.validateXPath(resp, 
         xpath);
  assertTrue("Debug documents does not contain child documents\n"+resp+"\n"+ xpath+
                                                      "\n"+results, results == null);
  
  assertTrue("Update request processor processAdd was not called", TestUpdateRequestProcessor.processAddCalled);
  assertTrue("Update request processor processCommit was not callled", TestUpdateRequestProcessor.processCommitCalled);
  assertTrue("Update request processor finish was not called", TestUpdateRequestProcessor.finishCalled);
  
  // very simple asserts to check that we at least have correct num of docs indexed
  assertQ(req("*:*"), "//*[@numFound='" + totalDocsNum + "']");
  assertQ(req("type_s:parent"), "//*[@numFound='" + parentsNum + "']");
  assertQ(req("type_s:child"), "//*[@numFound='" + childrenNum + "']");
  assertQ(req("type_s:grand_child"), "//*[@numFound='" + grandChildrenNum + "']");

  // let's check BlockJoin
  // get first parent by any grand children
  String randomGrandChildId = allGrandChildrenIds.get(random().nextInt(allGrandChildrenIds.size()));
  Query query = createToParentQuery(parentType, FIELD_ID, randomGrandChildId);
  assertSearch(query, FIELD_ID, parentId1);

  // get first parent by any children 
  String randomChildId = firstParentChildrenIds.get(random().nextInt(firstParentChildrenIds.size()));
  query = createToParentQuery(parentType, FIELD_ID, randomChildId);
  assertSearch(query, FIELD_ID, parentId1);
  
  // get parent by children by grand children
  randomGrandChildId = grandChildrenIds.get(random().nextInt(grandChildrenIds.size()));
  ToParentBlockJoinQuery childBlockJoinQuery = createToParentQuery(childType, FIELD_ID, randomGrandChildId);
  ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(childBlockJoinQuery, createParentFilter(parentType), ScoreMode.Avg);
  assertSearch(blockJoinQuery, FIELD_ID, parentId1);
}
 
Example 4
Source File: TestHierarchicalDocBuilder.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private ToParentBlockJoinQuery createToParentQuery(String parentType, Query childQuery) {
  ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(childQuery, createParentFilter(parentType), ScoreMode.Avg);
  
  return blockJoinQuery;
}