Example usage for org.springframework.data.solr.core.query HighlightOptions addField

List of usage examples for org.springframework.data.solr.core.query HighlightOptions addField

Introduction

In this page you can find the example usage for org.springframework.data.solr.core.query HighlightOptions addField.

Prototype

public HighlightOptions addField(String... fieldnames) 

Source Link

Document

Add names of fields to highlight on

Usage

From source file:com.nixmash.springdata.solr.repository.custom.CustomProductRepositoryImpl.java

@Override
public HighlightPage<Product> searchProductsWithHighlights(String searchTerm) {
    SimpleHighlightQuery query = new SimpleHighlightQuery();
    String[] words = searchTerm.split(" ");
    Criteria conditions = createHighlightedNameConditions(words);
    query.addCriteria(conditions);/*  ww w .ja  v a  2 s .  c o m*/

    HighlightOptions hlOptions = new HighlightOptions();
    hlOptions.addField("name");
    hlOptions.setSimplePrefix("<b>");
    hlOptions.setSimplePostfix("</b>");
    query.setHighlightOptions(hlOptions);

    return solrTemplate.queryForHighlightPage(query, Product.class);
}

From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java

@Test
public void testConstructSorlQueryWithFieldSpecificHighlightOptions() {
    SimpleHighlightQuery query = new SimpleHighlightQuery(new SimpleStringCriteria("field_1:value_1"));
    HighlightOptions options = new HighlightOptions();

    HighlightOptions.FieldWithHighlightParameters fieldWithHighlightParameters = new HighlightOptions.FieldWithHighlightParameters(
            "field_2");
    fieldWithHighlightParameters.setFormatter("formatter");
    fieldWithHighlightParameters.setFragsize(10);

    options.addField(fieldWithHighlightParameters);
    query.setHighlightOptions(options);/*  ww w. j a va2 s.co  m*/

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);
    Assert.assertArrayEquals(new String[] { "field_2" }, solrQuery.getHighlightFields());
    Assert.assertEquals(fieldWithHighlightParameters.getFormatter(),
            solrQuery.getParams("f.field_2." + HighlightParams.FORMATTER)[0]);
    Assert.assertEquals(fieldWithHighlightParameters.getFragsize().toString(),
            solrQuery.getParams("f.field_2." + HighlightParams.FRAGSIZE)[0]);
}