首页 > 解决方案 > ElasticSearch - 在我的输入查询中没有 (*) 就不会发生 JavaApi 搜索

问题描述

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

code : MS-VMA1615-0D

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

但是,如果我像下面这样输入,我不会得到结果。

Input : VMA1615         -- Am not getting the results.

我期待返回代码MS-VMA1615-0D

请在下面找到我正在使用的 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": "whitespace",
      "char_filter": [
        "html_strip"
      ],
      "filter": [
        "lowercase",
        "asciifolding"
      ]
    }
   }
  }
},
"mappings": {
"doc": {
  "properties": {
    "code": {
      "type": "text",
       "analyzer": "custom_analyzer"
      }
       }
  }
 }
}

标签: javaelasticsearchelastic-stack

解决方案


要执行您要查找的内容,您可能必须更改您正在使用的标记器。目前您正在使用必须用模式标记器替换的空白标记器。因此,您的新映射应如下所示:

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

因此,在更改映射后,对VMA1615的查询将返回MS-VMA1615-0D

这是因为它将字符串“MS-VMA1615-0D”标记为“MS”、“VMA1615”和“0D”。因此,只要在您的查询中有任何一个,它都会为您提供结果。

POST _analyze
{
  "tokenizer": "pattern",
  "text": "MS-VMA1615-0D"
}

将返回:

{
  "tokens": [
    {
      "token": "MS",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "VMA1615",
      "start_offset": 3,
      "end_offset": 10,
      "type": "word",
      "position": 1
    },
    {
      "token": "0D",
      "start_offset": 11,
      "end_offset": 13,
      "type": "word",
      "position": 2
    }
  ]
}

根据您的评论:

这不是弹性搜索的工作方式。Elasticsearch 将术语及其对应的文档存储在倒排索引数据结构中,默认情况下,全文搜索生成的术语基于空格,即文本“Hi there I am a technocrat”将拆分为 ["Hi ”、“那里”、“我”、“我”、“一个”、“技术官僚”]。因此,这意味着存储的术语取决于它的标记方式。在查询时建立索引后,假设在上面的示例中查询“technocrat”,我将得到结果,因为倒排索引具有与我的文档相关联的术语。因此,在您的情况下,“VMA”不会存储为术语。

为此,请使用以下映射:

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

所以要检查:

POST products/_analyze
{
  "tokenizer": "my_pattern_tokenizer",
  "text": "MS-VMA1615-0D"
}

将产生:

{
  "tokens": [
    {
      "token": "MS",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "VMA",
      "start_offset": 3,
      "end_offset": 6,
      "type": "word",
      "position": 1
    },
    {
      "token": "D",
      "start_offset": 12,
      "end_offset": 13,
      "type": "word",
      "position": 2
    }
  ]
}

推荐阅读