Skip to content

Commit

Permalink
Moving TermsLookupQueryBuilder to TermsQueryBuilder so we have 1:1 re…
Browse files Browse the repository at this point in the history
…lation to existing parser, leaving old class as deprecated stub extending TermsQueryBuilder
  • Loading branch information
cbuescher committed May 21, 2015
1 parent 5418664 commit 59305dc
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ public static TypeQueryBuilder typeQuery(String type) {
* A terms lookup filter for the provided field name. A lookup terms filter can
* extract the terms to filter by from another doc in an index.
*/
public static TermsLookupQueryBuilder termsLookupQuery(String name) {
return new TermsLookupQueryBuilder(name);
}
public static TermsQueryBuilder termsLookupQuery(String name, String lookupIndex, String lookupType, String lookupId, String lookupPath) {
return new TermsQueryBuilder(name, (Object[]) null).lookupIndex(lookupIndex).lookupType(lookupType).lookupId(lookupId).lookupPath(lookupPath);
}

/**
* A builder for filter based on a script.
Expand Down Expand Up @@ -674,7 +674,7 @@ public static GeohashCellQuery.Builder geoHashCellQuery(String name, GeoPoint po
public static GeohashCellQuery.Builder geoHashCellQuery(String name, String geohash, boolean neighbors) {
return new GeohashCellQuery.Builder(name, geohash, neighbors);
}

/**
* A filter to filter based on a polygon defined by a set of locations / points.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,109 +19,20 @@

package org.elasticsearch.index.query;

import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;

/**
* A filter for a field based on several terms matching on any of them.
* @deprecated use {@link TermsQueryBuilder#TermsQueryBuilder(name, lookupIndex, lookupType, lookupId)} instead.
*/
public class TermsLookupQueryBuilder extends QueryBuilder {

public static final String NAME = "terms_lookup";

private final String name;
private String lookupIndex;
private String lookupType;
private String lookupId;
private String lookupRouting;
private String lookupPath;
private Boolean lookupCache;

private String queryName;
@Deprecated
public class TermsLookupQueryBuilder extends TermsQueryBuilder {

public TermsLookupQueryBuilder(String name) {
this.name = name;
}

/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public TermsLookupQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}

/**
* Sets the index name to lookup the terms from.
*/
public TermsLookupQueryBuilder lookupIndex(String lookupIndex) {
this.lookupIndex = lookupIndex;
return this;
}

/**
* Sets the index type to lookup the terms from.
*/
public TermsLookupQueryBuilder lookupType(String lookupType) {
this.lookupType = lookupType;
return this;
}

/**
* Sets the doc id to lookup the terms from.
*/
public TermsLookupQueryBuilder lookupId(String lookupId) {
this.lookupId = lookupId;
return this;
}

/**
* Sets the path within the document to lookup the terms from.
*/
public TermsLookupQueryBuilder lookupPath(String lookupPath) {
this.lookupPath = lookupPath;
return this;
}

public TermsLookupQueryBuilder lookupRouting(String lookupRouting) {
this.lookupRouting = lookupRouting;
return this;
}

public TermsLookupQueryBuilder lookupCache(boolean lookupCache) {
this.lookupCache = lookupCache;
return this;
}

@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);

builder.startObject(name);
if (lookupIndex != null) {
builder.field("index", lookupIndex);
}
builder.field("type", lookupType);
builder.field("id", lookupId);
if (lookupRouting != null) {
builder.field("routing", lookupRouting);
}
if (lookupCache != null) {
builder.field("cache", lookupCache);
}
builder.field("path", lookupPath);
builder.endObject();

if (queryName != null) {
builder.field("_name", queryName);
}

builder.endObject();
super(name, (Object[]) null);
}

@Override
public String queryId() {
return NAME;
return TermsQueryBuilder.NAME;
}
}
91 changes: 77 additions & 14 deletions src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import java.io.IOException;

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*/
public class TermsQueryBuilder extends QueryBuilder {
public class TermsQueryBuilder extends QueryBuilder<TermsQueryBuilder> {

public static final String NAME = "terms";

Expand All @@ -38,8 +38,15 @@ public class TermsQueryBuilder extends QueryBuilder {

private String execution;

private String lookupIndex;
private String lookupType;
private String lookupId;
private String lookupRouting;
private String lookupPath;
private Boolean lookupCache;

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -49,7 +56,7 @@ public TermsQueryBuilder(String name, String... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -60,7 +67,7 @@ public TermsQueryBuilder(String name, int... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -71,7 +78,7 @@ public TermsQueryBuilder(String name, long... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -82,7 +89,7 @@ public TermsQueryBuilder(String name, float... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -93,7 +100,7 @@ public TermsQueryBuilder(String name, double... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand All @@ -104,7 +111,7 @@ public TermsQueryBuilder(String name, Object... values) {
}

/**
* A filer for a field based on several terms matching on any of them.
* A filter for a field based on several terms matching on any of them.
*
* @param name The field name
* @param values The terms
Expand Down Expand Up @@ -133,19 +140,75 @@ public TermsQueryBuilder queryName(String queryName) {
return this;
}

/**
* Sets the index name to lookup the terms from.
*/
public TermsQueryBuilder lookupIndex(String lookupIndex) {
this.lookupIndex = lookupIndex;
return this;
}

/**
* Sets the index type to lookup the terms from.
*/
public TermsQueryBuilder lookupType(String lookupType) {
this.lookupType = lookupType;
return this;
}

/**
* Sets the doc id to lookup the terms from.
*/
public TermsQueryBuilder lookupId(String lookupId) {
this.lookupId = lookupId;
return this;
}

/**
* Sets the path within the document to lookup the terms from.
*/
public TermsQueryBuilder lookupPath(String lookupPath) {
this.lookupPath = lookupPath;
return this;
}

public TermsQueryBuilder lookupRouting(String lookupRouting) {
this.lookupRouting = lookupRouting;
return this;
}

public TermsQueryBuilder lookupCache(boolean lookupCache) {
this.lookupCache = lookupCache;
return this;
}

@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field(name, values);

if (values == null) {
builder.startObject(name);
if (lookupIndex != null) {
builder.field("index", lookupIndex);
}
builder.field("type", lookupType);
builder.field("id", lookupId);
if (lookupRouting != null) {
builder.field("routing", lookupRouting);
}
if (lookupCache != null) {
builder.field("cache", lookupCache);
}
builder.field("path", lookupPath);
builder.endObject();
} else {
builder.field(name, values);
}
if (execution != null) {
builder.field("execution", execution);
}

if (queryName != null) {
builder.field("_name", queryName);
builder.field("_name", queryName);
}

builder.endObject();
}

Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/elasticsearch/count/query/CountQueryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,47 +639,47 @@ public void testTermsLookupFilter() throws Exception {
client().prepareIndex("test", "type", "4").setSource("term", "4"));

CountResponse countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "1", "terms"))).get();
assertHitCount(countResponse, 2l);

// same as above, just on the _id...
countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id", "lookup", "type", "1", "terms"))).get();
assertHitCount(countResponse, 2l);

// another search with same parameters...
countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "1", "terms"))).get();
assertHitCount(countResponse, 2l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "2", "terms"))).get();
assertHitCount(countResponse, 1l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("3").lookupPath("terms"))
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "3", "terms"))
).get();
assertNoFailures(countResponse);
assertHitCount(countResponse, 2l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "4", "terms"))).get();
assertHitCount(countResponse, 0l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "1", "arr.term"))).get();
assertHitCount(countResponse, 2l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "2", "arr.term"))).get();
assertHitCount(countResponse, 1l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "3", "arr.term"))).get();
assertHitCount(countResponse, 2l);

countResponse = client().prepareCount("test")
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get();
.setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists", "lookup2", "type", "3", "arr.term"))).get();
assertHitCount(countResponse, 0l);
}

Expand Down
Loading

0 comments on commit 59305dc

Please sign in to comment.