Java api for Filterbuilders in elasticsearch 2


(mramaprasad) #1

I am using
QueryBuilder qb = null;
FilterBuilder fb = null;

fb = FilterBuilders.andFilter(
FilterBuilders.termsFilter("dbname", searchDBs))

qb = (QueryBuilder) QueryBuilders.queryString("""+ searchText+ """).field("titlefield");

FilteredQueryBuilder builder = QueryBuilders.filteredQuery(qb,fb);

FunctionScoreQueryBuilder functionbuilder = new FunctionScoreQueryBuilder(builder).add(FilterBuilders.termsFilter("dbname", "test"), factorFunction(-30.0f))
.add(FilterBuilders.rangeFilter("pubdate").gte("2014-01"), factorFunction(10));

srb.setQuery(functionbuilder);

These don't work in elasticsearch 2.0.

Can I get some examples as to how to make these work in 2.0 please?


FilterBuilders v0.9 to v6.0 valid code replace
(David Pilato) #2

I think it does not compile at all.

Did you read https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-query-dsl.html ?

For example: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html#java-query-dsl-filtered-query

Note that it's better to use a must query here.


(mramaprasad) #3

Thank you!


(mramaprasad) #4

I updated the search query to the following. Each time i call setQuery it over writes the previous one. How do I rewrite this query?

QueryBuilder qb = null;
SearchRequestBuilder srb = null;

srb = client.prepareSearch("alias1");
srb = srb.setTypes("site");

qb = (QueryBuilder) QueryBuilders.queryString("""+ searchText+ """).field("titlefield");
srb.setQuery((org.elasticsearch.index.query.QueryBuilder) qb);

//for filtering
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

boolQueryBuilder.must(QueryBuilders.matchQuery("dbname", searchDBs));
boolQueryBuilder.must(QueryBuilders.rangeQuery("pubdate").from("1890-09").to("2016-08"));

QueryBuilder aScore = QueryBuilders.functionScoreQuery().
add(
QueryBuilders.matchQuery("dbname", "test"),

		       ScoreFunctionBuilders.weightFactorFunction(10)  
		   );

srb.setQuery(aScore)


(mramaprasad) #5

How to write the following filtered query using elasticsearch java api?

{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}

],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}


(system) #6