我有一个ES域,当我使用文档的emailId
字段进行查询时,我没有得到任何命中。但是,文档中存在此字段和值。对于同一个文档,按employeeId
进行查询是有效的。下面是我的索引映射的样子。
{
"properties": {
"employeeId": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"emailId": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
以下是我进行搜索的方式。
public SearchResponse searchForExactDocument(final String indexName, final Map<String, Object> queryMap)
throws IOException {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryMap.forEach((name, value) -> {
queryBuilder.must(QueryBuilders.termQuery(name, value));
});
return this.executeSearch(indexName, queryBuilder);
}
private SearchResponse executeSearch(final String indexName, final QueryBuilder queryBuilder) throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(indexName);
searchRequest.source(searchSourceBuilder);
return restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
}
我运行了SearcRequest.source((.toString((,下面是我得到的搜索的源字符串。
{
"query": {
"bool": {
"must": [
{
"term": {
"emailId": {
"value": "21june6lambdatest7@gmail.com",
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
下面是应该返回的文档,但没有得到任何点击。
index{
[
person
][
_doc
][
null
],
source[
{
"firstName": "MyEmployee",
"lastName": "June6Test7",
"emailId": "21june6lambdatest7@gmail.com",
"employeeId": "13908528"
}
]
}
我发现使用employeeId
的查询工作正常,但emailId
不工作,这很奇怪。任何帮助都将不胜感激。
更新:以下是我的索引创建方法。
public CreateIndexResponse createIndex(final CreateIndexInput createIndexInput) throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(createIndexInput.indexName());
Settings.Builder settingsBuilder = Settings.builder();
settingsBuilder.put(NUMBER_OF_SHARDS_KEY, createIndexInput.numOfShards());
settingsBuilder.put(NUMBER_OF_REPLICAS, createIndexInput.numOfReplicas());
settingsBuilder.put("analysis.analyzer.custom_uax_url_email.tokenizer", "uax_url_email");
createIndexInput.mapping().ifPresent(mapping ->
createIndexRequest.mapping(mapping, XContentType.JSON));
createIndexRequest.settings(settingsBuilder.build());
return restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}
Term查询返回在提供的字段中包含确切术语的文档。您需要将.keyword添加到emailId字段中。这使用关键字分析器而不是标准分析器(注意emailId字段后面的".keyword"(。
默认情况下,如果未指定分析器,text
类型字段将使用标准分析器。这将打破";21june6lambdatest7@gmail.com"转换为以下代币
{
"tokens": [
{
"token": "21june6lambdatest7",
"start_offset": 0,
"end_offset": 18,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "gmail.com",
"start_offset": 19,
"end_offset": 28,
"type": "<ALPHANUM>",
"position": 1
}
]
}
您需要将查询修改为
{
"query": {
"bool": {
"must": [
{
"term": {
"emailId.keyword": { // note this
"value": "21june6lambdatest7@gmail.com",
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
更新1:根据下面的评论,将您的索引映射和设置修改为
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "uax_url_email"
}
}
}
},
"mappings": {
"properties": {
"emailId": {
"type": "text",
"analyzer":"my_analyzer"
}
}
}
}
搜索查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"emailId": "21june6lambdatest7@gmail.com"
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
搜索结果:
"hits": [
{
"_index": "67823510",
"_type": "_doc",
"_id": "1",
"_score": 0.6931471,
"_source": {
"emailId": "21june6lambdatest7@gmail.com"
}
}
]