我已经索引了三个文件的元数据,它们是" text / plain" MIME类型。
但是当我尝试将其他mime-types
与" text / plain"匹配时,以下内容会匹配!
以下是mime-types
与" text / plain",hits
&匹配的列表score
:
***********************************
1. Mime-Type text/vnd.motorola.reflex
2. Total Hits 3
3. Max Score 0.07154637
***********************************
1. Mime-Type text/vnd.ms-mediapackage
2. Total Hits 3
3. Max Score 0.034633614
***********************************
1. Mime-Type text/vnd.net2phone.commcenter.command
2. Total Hits 3
3. Max Score 0.07154637
***********************************
1. Mime-Type text/plain
2. Total Hits 3
3. Max Score 0.629606
***********************************
我希望mime-type应完全匹配,并且应该只考虑最后一个。如果你发现它的Max-score高于上述所有值。
搜索代码:
query =" text / plain&#34 ;; filter =" mimeType"
public long getHitsCount(String query, String filter, Project project) {
try {
/*TermQueryBuilder QueryBuilder = new TermQueryBuilder(filter, smartEscapeQuery(query));*/
/* QueryStringQueryBuilder QueryBuilder = new QueryStringQueryBuilder(smartEscapeQuery(query)).field(filter);*/
MatchQueryBuilder QueryBuilder = QueryBuilders.matchQuery(filter, smartEscapeQuery(query));
QueryBuilder qb = QueryBuilders
.boolQuery()
.must(QueryBuilder);
SearchRequestBuilder requestBuilder;
requestBuilder = client.prepareSearch()
.setIndices(getDomainIndexId(project))
.setTypes(getProjectTypeId(project))
.setSearchType(SEARCH_TYPE)
.setQuery(qb);
SearchResponse response = requestBuilder.execute().actionGet(ES_TIMEOUT_MS);
SearchHits hits = response.getHits();
if (hits.getTotalHits() > 0) {
return hits.getTotalHits();
}else{
return 0l;
}
} catch (IndexMissingException ex) {
}
return 0;
}
/**
* Escape the string from bad chars for the search
*
* @param str the String that should be escaped
* @return an escaped String
*/
@SuppressWarnings({"ConstantConditions"})
private static String smartEscapeQuery(String str) {
if (StringUtils.isBlank(str)) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\\' || c == '+' || c == '-' || c == '!' || c ==
'(' || c == ')' || c == ':'
|| c == '^' || c == '[' || c == ']' || c == '\"'
|| c == '{' || c == '}' || c == '~' || c == '/'
|| c == '?' || c == '|' || c == '&' || c == ';'
|| (!Character.isSpaceChar(c) &&
Character.isWhitespace(c))) {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}
匹配查询:
{
"bool" : {
"must" : {
"match" : {
"mimeType" : {
"query" : "text\\/plain",
"type" : "boolean"
}
}
}
}
}
Result: 3 Hits
术语查询:
{
"bool" : {
"must" : {
"term" : {
"mimeType" : "text\\/plain"
}
}
}
}
Result: 0 Hits
我试过TermQuery
&amp; MatchQuery
但它不起作用。我在索引时使用AutoDetectParser
。
如何匹配elasticsearch中的确切值,以便在上面的示例中它只应与&#34; text / plain&#34;匹配。不匹配?
答案 0 :(得分:1)
在第一个示例中,您有一个类型为“匹配查询”的查询。因此,在搜索之前分析您的查询(文本或普通)。 您通过索引使用了哪种anlayzer?或者“not_analyzed”这个领域有用吗? 在第二个示例中,您使用“术语查询”类型。这也需要一个“not_analyzed”字段。