0

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

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.