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

时间:2018-07-07 11:52:10

标签: java elasticsearch elastic-stack

使用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

1 个答案:

答案 0 :(得分:1)

跟进: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查询。

在基巴纳州:

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