Boolean query with terms

Hi guys,

I am new to elasticsearch.

My application uses Lucene (v 4.0) to index records with a bunch of fields.
Then it uses a multi terms boolean query to search the top 10000 documents.

The code is something like this:

BooleanQuery query = new BooleanQuery();

for each field I need to search I do:

TokenStream tokenStream = analyzer.tokenStream(fieldName, new StringReader(
CharTermAttribute attr = tokenStream.getAttribute(CharTermAttribute.class);

while (tokenStream.incrementToken()) {
String term = attr.toString();
termQuery = new TermQuery(new Term(fieldName, term));

query.add(termQuery, Occur.SHOULD);

ScoreDoc[] hits =, filter, 10000).scoreDocs;

I am trying to port the same logic using elasticsearch. The code looks like:

BoolQueryBuilder bqb = QueryBuilders.boolQuery();

for (Fields field : myFields) {
String fieldName = field.getName();
Collection values = field.getValues();
if (values == null) {
for (String v : values) {

    TokenStream tokenStream = analyzer.tokenStream(fieldName, new 

CharTermAttribute attr = tokenStream.getAttribute(CharTermAttribute.

    while (tokenStream.incrementToken()) {
        String term = attr.toString();
        TermQueryBuilder tqb = QueryBuilders.termQuery(fieldName, term);
        bqb = bqb.should(tqb);



SearchResponse response = this.client.prepareSearch(this.indexName).setTypes

The elasticsearch query returns only the records with the exact match.
Instead I need to retrieve the documents close to my search request.
What am I doing wrong?

Thank you in advance.

You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit