ElasticSearch-JavaApi按每个字符而不是术语(单词)进行搜索 [英] ElasticSearch - JavaApi searching by each character instead of term (word)

查看:140
本文介绍了ElasticSearch-JavaApi按每个字符而不是术语(单词)进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java api从弹性搜索中获取文档时,我的弹性搜索文档中包含以下代码,并尝试使用以下模式进行搜索。

Am fetching documents from elastic search using java api, i have the following code in my elastic search documents and am trying to search it with the following pattern.

代码: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) .

但是,如果我输入以下内容,则不会得到结果。

But, if i give input like below, am not getting results.

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 。简单来说,就是尝试逐字符而不是术语(单词)进行搜索。

Am expecting to return the code MS-VMA1615-0D. In simple, am trying to search character by character instead of term (word).

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

It should not return the code MS-VMA1615-0D for the following cases, Because its not matching with my code.

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

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

Please find my below java code that am using

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

请找到我的地图详细信息:

Please find my mapping details :

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的请求

This is my request via 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查询。

And you should be using a match_phrase query.

在基巴纳州:

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": []
  }
}

根据您的评论:

而不是使用查询字符串:

Instead of using a Query string:

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

这篇关于ElasticSearch-JavaApi按每个字符而不是术语(单词)进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆