ElasticSearch - JavaApi 按每个字符而不是术语(单词)进行搜索



正在使用java API从弹性搜索中获取文档,我的弹性搜索文档中有以下code,并尝试使用以下模式进行搜索。

code : MS-VMA1615-0D

Input : MS-VMA1615-0D   -- Am getting the results (MS-VMA1615-0D).
Input : VMA1615         -- Am getting the results (MS-VMA1615-0D) .
Input : VMA             -- Am getting the results (MS-VMA1615-0D) .

但是,如果我给出如下输入,则不会得到结果。

Input : V       -- Am not getting the results.
INPUT : MS      -- Am not getting the results.
INPUT : -V      -- Am not getting the results.
INPUT : 615     -- Am not getting the results.

我希望返回代码MS-VMA1615-0D。简单来说,我试图逐个字符而不是术语(单词(搜索。

对于以下情况,它不应该返回代码MS-VMA1615-0D,因为它与我的代码不匹配。

Input : VK      -- should not return the results.
INPUT : MS3     -- should not return the results.

请在下面找到我正在使用的 java 代码

private final String INDEX = "products";
private final String TYPE = "doc";
SearchRequest searchRequest = new SearchRequest(INDEX); 
searchRequest.types(TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder qsQueryBuilder = new QueryStringQueryBuilder(code); 
qsQueryBuilder.defaultField("code");
searchSourceBuilder.query(qsQueryBuilder);
searchSourceBuilder.size(50);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = null;
try {
searchResponse = SearchEngineClient.getInstance().search(searchRequest);
} catch (IOException e) {
e.getLocalizedMessage();
}
Item item = null;
SearchHit[] searchHits = searchResponse.getHits().getHits();

请找到我的映射详细信息:

PUT products
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "my_pattern_tokenizer",
"char_filter": [
"html_strip"
],
"filter": [
"lowercase",
"asciifolding"
]
}
},
"tokenizer": {
"my_pattern_tokenizer": {
"type": "pattern",
"pattern": "-|\d"
}
}
}
},
"mappings": {
"doc": {
"properties": {
"code": {
"type": "text",
"analyzer": "custom_analyzer"
}
}
}
}
}

更新后使用新答案:

这是我通过Java API的请求

'SearchRequest{searchType=QUERY_THEN_FETCH, indices=[products], indicesOptions=IndicesOptions[id=38, ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, expand_wildcards_closed=false, allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false], types=[doc], routing='null', preference='null', requestCache=null, scroll=null, maxConcurrentShardRequests=0, batchedReduceSize=512, preFilterShardSize=128, source={"size":50,"query":{"match_phrase":{"code":{"query":"1615","slop":0,"boost":1.0}}}}}

'。但我得到的回应是null

跟进:ElasticSearch - JavaApi搜索在我的输入查询中没有(*(就不会发生

您的映射应如下所示:

PUT products
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "ngram",
"char_filter": [
"html_strip"
],
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"code": {
"type": "text",
"analyzer": "custom_analyzer"
}
}
}
}
}

您应该使用match_phrase查询。

在木花中:

GET products/_search
{
"query": {
"match_phrase": {
"code": "V"
}
}
}

将返回结果:

"hits": [
{
"_index": "products",
"_type": "doc",
"_id": "EoGtdGQBqdof7JidJkM_",
"_score": 0.2876821,
"_source": {
"code": "MS-VMA1615-0D"
}
}
]

但是这个:

GET products/_search
{
"query": {
"match_phrase": {
"code": "VK"
}
}
}

习惯:

{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

根据您的评论:

而不是使用查询字符串:

QueryStringQueryBuilder qsQueryBuilder = new QueryStringQueryBuilder(code); 
qsQueryBuilder.defaultField("code");
searchSourceBuilder.query(qsQueryBuilder);
searchSourceBuilder.size(50);
searchRequest.source(searchSourceBuilder);

使用匹配短语查询:

QueryBuilder query = QueryBuilders.matchPhraseQuery("code", code);
searchSourceBuilder.query(query);
searchSourceBuilder.size(50);
searchRequest.source(searchSourceBuilder);

最新更新