gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:02:29 27 4
gpt4 key购买 nike

我使用 java api 从 Elasticsearch 中获取文档,我的 Elasticsearch 文档中有以下代码,并尝试使用以下模式搜索它。

代码: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 searching not happening without (*) in my input query

您的映射应如下所示:

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 查询。

在 Kibana 中:

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

关于java - ElasticSearch - JavaApi 按每个字符而不是术语(单词)进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com