Boolean query with terms


(Fabrizio Fortino) #1

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


(system) #2