2
votes

If the query was Brid I want to get <em>Brid</em>gitte in highlighted fields, not the whole word <em>Bridgitte</em>

My index looks like this (I've added ngram analyzer as was suggested here Highlighting part of word in elasticsearch)

{
"myindex": {
    "aliases": {},
    "mappings": {
        "mytype": {
            "properties": {
                "myarrayproperty": {
                    "properties": {
                        "mystringproperty1": {
                            "type": "string",
                            "term_vector": "with_positions_offsets",
                            "analyzer": "index_ngram_analyzer",
                            "search_analyzer": "search_term_analyzer"
                        },
                        "mystringproperty2": {
                            "type": "string",
                            "term_vector": "with_positions_offsets",
                            "analyzer": "index_ngram_analyzer",
                            "search_analyzer": "search_term_analyzer"
                        }
                    },
                    "mylongproperty": {
                        "type": "long"
                    },
                    "mydateproperty": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "mystringproperty3": {
                        "type": "string",
                        "term_vector": "with_positions_offsets",
                        "analyzer": "index_ngram_analyzer",
                        "search_analyzer": "search_term_analyzer"
                    },
                    "mystringproperty4": {
                        "type": "string",
                        "term_vector": "with_positions_offsets",
                        "analyzer": "index_ngram_analyzer",
                        "search_analyzer": "search_term_analyzer"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1498030893611",
                "analysis": {
                    "analyzer": {
                        "search_term_analyzer": {
                            "filter": "lowercase",
                            "type": "custom",
                            "tokenizer": "ngram_tokenizer"
                        },
                        "index_ngram_analyzer": {
                            "filter": ["lowercase"],
                            "type": "custom",
                            "tokenizer": "ngram_tokenizer"
                        }
                    },
                    "tokenizer": {
                        "ngram_tokenizer": {
                            "token_chars": ["letter", "digit"],
                            "min_gram": "1",
                            "type": "nGram",
                            "max_gram": "15"
                        }
                    }
                },
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "e5kBX-XRTKOqeAScO1Fs0w",
                "version": {
                    "created": "2040499"
                }
            }
        },
        "warmers": {}
    }
}

}

This is embedded Elasticsearch instance, not sure if it's relevant.

My query looks like this

    MatchQueryBuilder queryBuilder = matchPhrasePrefixQuery("_all", query).maxExpansions(50);
final SearchResponse response = client.prepareSearch("myindex")
    .setQuery(queryBuilder)
    .addHighlightedField("mystringproperty3", 0, 0)
    .addHighlightedField("mystringproperty4", 0, 0)
    .addHighlightedField("myarrayproperty.mystringproperty1", 0, 0)
    .setHighlighterRequireFieldMatch(false)
    .execute().actionGet();

And it doesn't work. I've tried to change query to queryStringQuery but it seems like it doesn't support search by part of the word. Any suggestions?

1

1 Answers

2
votes

It's not possible. Elastic search does the indexing of a word. From tokenization perspective, you can not do much here.

You may need to write the wrapper over a search result. (Not elastic search specific)