# Boolean query with terms

**URL:** <https://discuss.elastic.co/t/boolean-query-with-terms/16457>\
**Category:** Elasticsearch\
**Created:** [March 19, 2014, 10:09am UTC](https://discuss.elastic.co/t/boolean-query-with-terms/16457 "2014-03-19T10:09:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabrizio\_Fortino](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabrizio_fortino/32/1719_2.png) [@Fabrizio\_Fortino](https://discuss.elastic.co/u/Fabrizio_Fortino)\
**Post date:** [March 19, 2014, 10:09am UTC](https://discuss.elastic.co/t/boolean-query-with-terms/16457/1 "2014-03-19T10:09:11Z")

</div>

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(  
fieldValue));  
tokenStream.reset();  
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 = searcher.search(query, 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) {  
continue;  
}  
for (String v : values) {

```
    TokenStream tokenStream = analyzer.tokenStream(fieldName, new 

```

StringReader(v));  
tokenStream.reset();  
CharTermAttribute attr = tokenStream.getAttribute(CharTermAttribute.  
class);

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

    }
    tokenStream.close();
}

```

}

SearchResponse response = this.client.prepareSearch(this.indexName).setTypes  
(this.indexType)  
.setSearchType(SearchType.DFS\_QUERY\_AND\_FETCH).setQuery(bqb).setSize(this.  
maxSearchHits).setExplain(true).execute().actionGet();

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.  
F.

--  
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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0de05c9f-7294-440f-a814-8bb6e7f49060%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0de05c9f-7294-440f-a814-8bb6e7f49060%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:42am UTC](https://discuss.elastic.co/t/boolean-query-with-terms/16457/2 "2017-07-06T01:42:15Z")

</div>


