List of usage examples for org.apache.lucene.search.join ScoreMode Total
ScoreMode Total
To view the source code for org.apache.lucene.search.join ScoreMode Total.
Click Source Link
From source file:edu.mayo.cts2.framework.plugin.service.lexevs.service.codesystemversion.LexEvsCodeSystemVersionQueryServiceTestIT.java
License:Open Source License
@Test public void queryPropertyTest() throws ParseException { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new TermQuery(new Term("isParentDoc", "true")), Occur.MUST_NOT); builder.add(new TermQuery(new Term("code", "C99998")), Occur.MUST); builder.add(new TermQuery(new Term("propertyName", "Contributing_Source")), Occur.MUST); QueryParser propValueParser = new QueryParser("propertyValue", sourceAssertedValueSetSearchIndexService.getAnalyzer()); builder.add(propValueParser.createBooleanQuery("propertyValue", "FDA"), Occur.MUST); Query query = builder.build(); QueryBitSetProducer parentFilter;// ww w. j a va 2 s. c om parentFilter = new QueryBitSetProducer( new QueryParser("isParentDoc", new StandardAnalyzer(new CharArraySet(0, true))).parse("true")); ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(query, parentFilter, ScoreMode.Total); List<ScoreDoc> docs = sourceAssertedValueSetSearchIndexService.query(null, blockJoinQuery); assertNotNull(docs); assertTrue(docs.size() > 0); ScoreDoc sd = docs.get(0); Document doc = sourceAssertedValueSetSearchIndexService.getById(sd.doc); assertNotNull(doc); boolean fieldFound = false; List<IndexableField> fields = doc.getFields(); for (IndexableField field : fields) { if (field.name().equals("entityCode") && field.stringValue().equals("C99998")) { fieldFound = true; } } assertTrue(fieldFound); }
From source file:edu.mayo.cts2.framework.plugin.service.lexevs.service.codesystemversion.LexEvsCodeSystemVersionQueryServiceTestIT.java
License:Open Source License
@Test public void queryPublishPropertyTest() throws ParseException { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new TermQuery(new Term("isParentDoc", "true")), Occur.MUST_NOT); builder.add(new TermQuery(new Term("code", "C99999")), Occur.MUST); builder.add(new TermQuery(new Term("propertyName", "Publish_Value_Set")), Occur.MUST); QueryParser propValueParser = new QueryParser("propertyValue", sourceAssertedValueSetSearchIndexService.getAnalyzer()); builder.add(propValueParser.createBooleanQuery("propertyValue", "Yes"), Occur.MUST); Query query = builder.build(); QueryBitSetProducer parentFilter;//from w ww.ja v a 2 s .c om parentFilter = new QueryBitSetProducer( new QueryParser("isParentDoc", new StandardAnalyzer(new CharArraySet(0, true))).parse("true")); ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(query, parentFilter, ScoreMode.Total); List<ScoreDoc> docs = sourceAssertedValueSetSearchIndexService.query(null, blockJoinQuery); assertNotNull(docs); assertTrue(docs.size() > 0); ScoreDoc sd = docs.get(0); Document doc = sourceAssertedValueSetSearchIndexService.getById(sd.doc); assertNotNull(doc); boolean fieldFound = false; List<IndexableField> fields = doc.getFields(); for (IndexableField field : fields) { if (field.name().equals("entityCode") && field.stringValue().equals("C99999")) { fieldFound = true; } } assertTrue(fieldFound); }
From source file:org.codelibs.elasticsearch.index.query.HasChildQueryBuilder.java
License:Apache License
public static ScoreMode parseScoreMode(String scoreModeString) { if ("none".equals(scoreModeString)) { return ScoreMode.None; } else if ("min".equals(scoreModeString)) { return ScoreMode.Min; } else if ("max".equals(scoreModeString)) { return ScoreMode.Max; } else if ("avg".equals(scoreModeString)) { return ScoreMode.Avg; } else if ("sum".equals(scoreModeString)) { return ScoreMode.Total; }//from w w w.j ava2s . c o m throw new IllegalArgumentException("No score mode for child query [" + scoreModeString + "] found"); }
From source file:org.codelibs.elasticsearch.index.query.HasChildQueryBuilder.java
License:Apache License
public static String scoreModeAsString(ScoreMode scoreMode) { if (scoreMode == ScoreMode.Total) { // Lucene uses 'total' but 'sum' is more consistent with other elasticsearch APIs return "sum"; } else {/*w ww . java 2s .c o m*/ return scoreMode.name().toLowerCase(Locale.ROOT); } }
From source file:org.elasticsearch.index.query.HasChildQueryBuilderTests.java
License:Apache License
public void testSumFromString() { assertThat("fromString(total) != SUM", ScoreMode.Total, equalTo(HasChildQueryBuilder.parseScoreMode("sum"))); assertThat("sum", equalTo(HasChildQueryBuilder.scoreModeAsString(ScoreMode.Total))); }
From source file:org.elasticsearch.index.query.HasChildQueryParserTests.java
License:Apache License
public void testSumFromString() { assertThat("fromString(total) != SUM", ScoreMode.Total, equalTo(HasChildQueryParser.parseScoreMode("total"))); }
From source file:org.elasticsearch.index.query.NestedQueryParser.java
License:Apache License
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); Query query = null;//from w w w . jav a 2s. c o m boolean queryFound = false; Filter filter = null; boolean filterFound = false; float boost = 1.0f; String path = null; ScoreMode scoreMode = ScoreMode.Avg; String queryName = null; // we need a late binding filter so we can inject a parent nested filter inner nested queries LateBindingParentFilter currentParentFilterContext = parentFilterContext.get(); LateBindingParentFilter usAsParentFilter = new LateBindingParentFilter(); parentFilterContext.set(usAsParentFilter); try { 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)) { queryFound = true; query = parseContext.parseInnerQuery(); } else if ("filter".equals(currentFieldName)) { filterFound = true; filter = parseContext.parseInnerFilter(); } else { throw new QueryParsingException(parseContext.index(), "[nested] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { if ("path".equals(currentFieldName)) { path = parser.text(); } else if ("boost".equals(currentFieldName)) { boost = parser.floatValue(); } else if ("_scope".equals(currentFieldName)) { throw new QueryParsingException(parseContext.index(), "the [_scope] support in [nested] query has been removed, use nested filter as a facet_filter in the relevant facet"); } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) { String sScoreMode = parser.text(); if ("avg".equals(sScoreMode)) { scoreMode = ScoreMode.Avg; } 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.index(), "illegal score_mode for nested query [" + sScoreMode + "]"); } } else if ("_name".equals(currentFieldName)) { queryName = parser.text(); } else { throw new QueryParsingException(parseContext.index(), "[nested] query does not support [" + currentFieldName + "]"); } } } if (!queryFound && !filterFound) { throw new QueryParsingException(parseContext.index(), "[nested] requires either 'query' or 'filter' field"); } if (path == null) { throw new QueryParsingException(parseContext.index(), "[nested] requires 'path' field"); } if (query == null && filter == null) { return null; } if (filter != null) { query = new XConstantScoreQuery(filter); } MapperService.SmartNameObjectMapper mapper = parseContext.smartObjectMapper(path); if (mapper == null) { throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]"); } ObjectMapper objectMapper = mapper.mapper(); if (objectMapper == null) { throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]"); } if (!objectMapper.nested().isNested()) { throw new QueryParsingException(parseContext.index(), "[nested] nested object under path [" + path + "] is not of nested type"); } Filter childFilter = parseContext.cacheFilter(objectMapper.nestedTypeFilter(), null); usAsParentFilter.filter = childFilter; // wrap the child query to only work on the nested path type query = new XFilteredQuery(query, childFilter); Filter parentFilter = currentParentFilterContext; if (parentFilter == null) { parentFilter = NonNestedDocsFilter.INSTANCE; // don't do special parent filtering, since we might have same nested mapping on two different types //if (mapper.hasDocMapper()) { // // filter based on the type... // parentFilter = mapper.docMapper().typeFilter(); //} parentFilter = parseContext.cacheFilter(parentFilter, null); } ToParentBlockJoinQuery joinQuery = new ToParentBlockJoinQuery(query, parentFilter, scoreMode); joinQuery.setBoost(boost); if (queryName != null) { parseContext.addNamedQuery(queryName, joinQuery); } return joinQuery; } finally { // restore the thread local one... parentFilterContext.set(currentParentFilterContext); } }
From source file:org.elasticsearch.index.search.ESToParentBlockJoinQueryTests.java
License:Apache License
public void testEquals() { Query q1 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "child")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested"); Query q2 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "child")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested"); assertEquals(q1, q2);// w ww. j av a 2 s .c o m assertEquals(q1.hashCode(), q2.hashCode()); Query q3 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "not_child")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested"); assertFalse(q1.equals(q3)); assertFalse(q1.hashCode() == q3.hashCode()); Query q4 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "child")), new QueryBitSetProducer(new TermQuery(new Term("is", "other_parent"))), ScoreMode.Avg, "nested"); assertFalse(q1.equals(q4)); assertFalse(q1.hashCode() == q4.hashCode()); Query q5 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "child")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Total, "nested"); assertFalse(q1.equals(q5)); assertFalse(q1.hashCode() == q5.hashCode()); Query q6 = new ESToParentBlockJoinQuery(new TermQuery(new Term("is", "child")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested2"); assertFalse(q1.equals(q6)); assertFalse(q1.hashCode() == q6.hashCode()); }
From source file:org.elasticsearch.join.query.ChildQuerySearchIT.java
License:Apache License
public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { if (legacy()) { assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent") .addMapping("child1", "_parent", "type=parent")); } else {//from ww w.j a va 2s . c o m assertAcked(prepareCreate("test").addMapping("doc", jsonBuilder().startObject().startObject("doc").startObject("properties") .startObject("join_field").field("type", "join").startObject("relations") .field("parent", new String[] { "child", "child1" }).endObject().endObject().endObject() .endObject().endObject())); } ensureGreen(); indexRandom(true, createDocBuilders().toArray(new IndexRequestBuilder[0])); SearchResponse response = client().prepareSearch("test") .setQuery(hasChildQuery("child", QueryBuilders .functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) .boostMode(CombineFunction.REPLACE), ScoreMode.Total)) .get(); assertThat(response.getHits().getTotalHits(), equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("1")); assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f)); assertThat(response.getHits().getHits()[1].getId(), equalTo("3")); assertThat(response.getHits().getHits()[1].getScore(), equalTo(4f)); assertThat(response.getHits().getHits()[2].getId(), equalTo("2")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(3f)); response = client().prepareSearch("test") .setQuery(hasChildQuery("child", QueryBuilders .functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) .boostMode(CombineFunction.REPLACE), ScoreMode.Max)) .get(); assertThat(response.getHits().getTotalHits(), equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("3")); assertThat(response.getHits().getHits()[0].getScore(), equalTo(4f)); assertThat(response.getHits().getHits()[1].getId(), equalTo("2")); assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f)); assertThat(response.getHits().getHits()[2].getId(), equalTo("1")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(2f)); response = client().prepareSearch("test") .setQuery(hasChildQuery("child", QueryBuilders .functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) .boostMode(CombineFunction.REPLACE), ScoreMode.Avg)) .get(); assertThat(response.getHits().getTotalHits(), equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("3")); assertThat(response.getHits().getHits()[0].getScore(), equalTo(4f)); assertThat(response.getHits().getHits()[1].getId(), equalTo("2")); assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f)); assertThat(response.getHits().getHits()[2].getId(), equalTo("1")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(1.5f)); response = client().prepareSearch("test") .setQuery(hasParentQuery("parent", QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), fieldValueFactorFunction("p_field2")).boostMode(CombineFunction.REPLACE), true)) .addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get(); assertThat(response.getHits().getTotalHits(), equalTo(7L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("16")); assertThat(response.getHits().getHits()[0].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[1].getId(), equalTo("17")); assertThat(response.getHits().getHits()[1].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[2].getId(), equalTo("18")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[3].getId(), equalTo("19")); assertThat(response.getHits().getHits()[3].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[4].getId(), equalTo("20")); assertThat(response.getHits().getHits()[4].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[5].getId(), equalTo("21")); assertThat(response.getHits().getHits()[5].getScore(), equalTo(5f)); assertThat(response.getHits().getHits()[6].getId(), equalTo("22")); assertThat(response.getHits().getHits()[6].getScore(), equalTo(5f)); }
From source file:org.elasticsearch.join.query.ChildQuerySearchIT.java
License:Apache License
public void testReIndexingParentAndChildDocuments() throws Exception { if (legacy()) { assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent")); } else {/*from ww w. j ava 2s. c o m*/ assertAcked(prepareCreate("test").addMapping("doc", buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); } ensureGreen(); // index simple data createIndexRequest("test", "parent", "p1", null, "p_field", "p_value1").get(); createIndexRequest("test", "child", "c1", "p1", "c_field", "red").get(); createIndexRequest("test", "child", "c2", "p1", "c_field", "yellow").get(); createIndexRequest("test", "parent", "p2", null, "p_field", "p_value2").get(); createIndexRequest("test", "child", "c3", "p2", "c_field", "x").get(); createIndexRequest("test", "child", "c4", "p2", "c_field", "x").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1\"")); searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchQuery("c_field", "x")) .must(hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c3")); assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("c4")); // re-index for (int i = 0; i < 10; i++) { createIndexRequest("test", "parent", "p1", null, "p_field", "p_value1").get(); createIndexRequest("test", "child", "d" + i, "p1", "c_field", "red").get(); createIndexRequest("test", "parent", "p2", null, "p_field", "p_value2").get(); createIndexRequest("test", "child", "c3", "p2", "c_field", "x").get(); client().admin().indices().prepareRefresh("test").get(); } searchResponse = client().prepareSearch("test") .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1\"")); searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchQuery("c_field", "x")) .must(hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); assertThat(searchResponse.getHits().getAt(1).getId(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); }