两种搜索方式?

编程入门 行业动态 更新时间:2024-10-26 10:28:58
本文介绍了两种搜索方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究一个使用Elasticsearch 5.2的项目.该代码在Java中,我使用Elasticsearch Java Client 5.2.

I work on a project that uses elasticsearch 5.2. The code is in java and I use elasticsearch java client 5.2.

在这个项目中,我有一个名为hash的字段,它是一个7个字符的代码,其中包含大写字母,小写字母和数字(英语).我想在此字段上进行两次搜索:

In this project, I have a field called hash, a 7-character code containing uppercase letters, lowercase letters, and numbers (in English language). I want to do two searches on this field:

  • 检查是否存在哈希"ErTg1Qh" (区分大小写)

    找到子字符串s包含在其中的哈希(例如,子字符串"tg" 存在于哈希"ErTg1Qh" 中).

    find hashes that sub string s contains in them (for example, sub string "tg" exist in the hash "ErTg1Qh").

    对于散列字段,我选择了关键字数据类型.

    For the hash field, I chose the keyword datatype.

    我使用matchQuery函数进行第一次搜索,如下所示:

    I used matchQuery function for first search as below:

    String hash = "ErTg1Qh"; QueryBuilders.matchQuery("hash", hash)

    和queryStringQuery函数用于第二次搜索,如下所示:

    and queryStringQuery function for the second search as below:

    queryString = "hash:*" + subString + "*"; QueryBuilders.queryStringQuery(queryString)

    但是,第二个不能正常工作.

    but, the second one does not work properly.

    如何在字段上处理这两种搜索?

    How can I handle these two kinds of search on a field?

    推荐答案

    您的一个查询要求区分大小写,而第二个查询则不区分大小写.因此,我建议您将子字段用作哈希字段.您的主字段将使用小写分析器进行分析,并且其中一个字段将存储原始数据,即确切的哈希值.因此,您的索引将如下所示:

    One of your query requires to be case sensitive while the second one is case insensitive. So I'll suggest you to use sub field for the hash field. Your main field will be analyzed with lowercase analyzer and one will store raw data i.e. the exact hash. So your index will look like below:

    PUT /test { "settings": { "number_of_shards": "1", "number_of_replicas": "0", "analysis": { "analyzer": { "custom_lowercase": { "filter": [ "lowercase" ], "type": "custom", "tokenizer": "keyword" } } } }, "mappings": { "_doc": { "properties": { "hash": { "type": "text", "analyzer": "custom_lowercase", "fields": { "keyword": { "type": "keyword" } } } } } } }

    查询以检查是否存在哈希"ErTg1Qh"(区分大小写)

    Query to check the existence of a hash "ErTg1Qh" (case sensitive)

    POST /test/_doc/_search { "query": { "match": { "hash.keyword": "ErTg1Qh" } } }

    查询以查找子字符串s中包含的哈希

    Query to find hashes that sub string s contains in them

    POST /test/_doc/_search { "query": { "query_string": { "query": "*tg*" } } }
  • 更多推荐

    两种搜索方式?

    本文发布于:2023-10-26 21:49:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1531444.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:两种   方式

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!