【问题标题】:ElasticSearch - JavaApi searching not happening without (*) in my input queryElasticSearch - 在我的输入查询中没有 (*) 就不会发生 JavaApi 搜索
【发布时间】:2018-07-06 14:34:11
【问题描述】:

我正在使用 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"
      }
       }
  }
 }
}

【问题讨论】:

    标签: java elasticsearch elastic-stack


    【解决方案1】:

    要执行您要查找的内容,您可能需要更改您正在使用的标记器。目前您正在使用 whitespace 标记器,必须将其替换为 pattern 标记器。 所以你的新映射应该如下所示:

    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"]。所以这意味着 存储的术语取决于它的标记方式。后 查询时建立索引让我们在上面的示例中说,如果我查询 “技术专家”,我会得到结果,因为倒排索引有 与我的文档相关的术语。因此,在您的情况下,“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
        }
      ]
    }
    

    【讨论】:

    • 如果正在搜索 VMA,此模式不起作用。是否可以使其仅可搜索 VMA...?
    • 是的,如果您不担心 VMA 之后的数字,即“1616”,这是可能的?
    • 如果您不担心数字,那么我可以更改答案。告诉我!
    • Yes Please.,它的类似,排列和组合。我也以VMA 的形式提供输入,并期望它应该返回与我的输入相关的内容,因为它在 Elastic Search 中可用。我认为这是有道理的。
    • 更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2017-04-02
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多