弹性搜索突出显示整个单词而不是单词的一部分

时间:2017-06-21 08:15:43

标签: elasticsearch autocomplete

如果查询为Brid,我希望在突出显示的字段中显示<em>Brid</em>gitte,而不是整个字<em>Bridgitte</em>

我的索引看起来像这样(我已经按照Highlighting part of word in elasticsearch的建议添加了ngram分析器)

{
"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": {}
    }
}

}

这是嵌入式Elasticsearch实例,不确定它是否相关。

我的查询看起来像这样

    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();

它不起作用。我试图将查询更改为queryStringQuery,但似乎它并不支持通过部分单词进行搜索。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是不可能的。弹性搜索可以对单词进行索引。从标记化的角度来看,你在这里做不了多少。

您可能需要在搜索结果上编写包装器。 (非弹性搜索特定)