我正在研究一个使用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*" } } }更多推荐
两种搜索方式?
发布评论