I have an index in ElasticSearch (customers) that has the following partial mapping:
"addresses": {
"type": "nested",
"properties": {
"address": {
"type": "keyword"
},
"phone": {
"type": "keyword"
}
}
},
I am trying to search for a specific address (let's call it "foo street"). I am using ElasticSearch 6.3. and the RestHighLevelClient(6.3).
In my case, the following variables have the following values:
docName = "addresses"
name = "address"
value = "foo street"
I know that at least one of the documents in my customer index has an address of "foo street"
My code to create the NestedQueryBuilder is this:
QueryBuilder innerQuery = QueryBuilders.matchQuery(name, value);
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery(
docName, innerQuery, ScoreNode.None);
SearchRequest request = new SearcRequest("customers");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(nestedQueryBuilder);
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
request.source(searchSourceBuilder);
If I log my NestedQueryBuilder object (calling toString(), this is what I get:
{
"nested" : {
"query" : {
"match" : {
"ip_address" : {
"query" : "10.214.159.193",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
"path" : "links",
"ignore_unmapped" : false,
"score_mode" : "none",
"boost" : 1.0
}
}
Why am I not getting any hits?
The following is the value in SearchResponse:
Status: OK
Took: 0s
terminatedEarly: null
timedOut: false
total hits: 0
maxScore: NaN
You should use fully qualified name for nested fields. Use links.ip_address
instead of ip_address
.
You query should be as below:
{
"nested": {
"query": {
"match": {
"links.ip_address": { <--------------------- change here
"query": "10.214.159.193",
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
"path": "links",
"ignore_unmapped": false,
"score_mode": "none",
"boost": 1
}
}
In code:
String fullName = docName + "." + name;
QueryBuilder innerQuery = QueryBuilders.matchQuery(fullName, value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.