Yash Tandon
Yash Tandon

Reputation: 475

Query regarding functionScoreQuery of elastic-builder npm for elasticsearch

I am using a functionScoreQuery provided by the elastic-builder npm to query my elasticsearch, query is getting created but i am not able to get outer query params for my query as shown below.

i.e the outer query params are missing and that is why the query does not execute so i had to manually append query { } in my body. So if anyone can help me out and tell me what i am missing in my npm query to get those query params.

var not_body = elasticbuilder.functionScoreQuery()
                         .query(elasticbuilder.matchAllQuery())
                         .functions([
                                            elasticbuilder.weightScoreFunction()
                                              .filter(elasticbuilder.boolQuery().mustNot([
                                                elasticbuilder.hasChildQuery(
                                                    elasticbuilder.boolQuery().must([
                                                      elasticbuilder.matchPhraseQuery("name", "raju" )
                                                    ])
                                                ).type('student')
                                              ]))
                                              .weight(2),

                                            elasticbuilder.weightScoreFunction()
                                              .filter(elasticbuilder.boolQuery().must([
                                                  elasticbuilder.hasChildQuery(
                                                      elasticbuilder.boolQuery().must([
                                                          elasticbuilder.matchPhraseQuery("class", "12")
                                                        ])
                                                  ).type('info')                                                  
                                              ]))
                                            .weight(2)
                        ]).minScore(4).scoreMode('sum');

Current Output body via this query:

  {
   "function_score": {
   "functions": [
  {
    "filter": {
      "bool": {
        "must_not": {
          "has_child": {
            "query": {
              "bool": {
                "must": {
                  "match_phrase": {
                    "name" : "raju"
                  }
                }
              }
            },
            "type": "student"
          }
        }
      }
    },
    "weight": 2
  },
  {
    "filter": {
      "bool": {
        "must": {
          "has_child": {
            "query": {
              "bool": {
                "must": {
                  "match_phrase": {
                    "class" : "12"
                  }
                }
              }
            },
            "type": "info"
          }
        }
      }
    },
    "weight": 2
  }
],
"query": {
  "match_all": {}
},
"min_score": 4,
"score_mode": "sum"
}
}

Expected Output body:

{    
"query": {
"function_score": {
  "functions": [
    {
      "filter": {
        "bool": {
          "must_not": {
            "has_child": {
              "query": {
                "bool": {
                  "must": {
                    "match_phrase": {
                      "name" : "raju"
                    }
                  }
                }
              },
              "type": "student"
            }
          }
        }
      },
      "weight": 2
    },
    {
      "filter": {
        "bool": {
          "must": {
            "has_child": {
              "query": {
                "bool": {
                  "must": {
                    "match_phrase": {
                      "class" : "12"
                    }
                  }
                }
              },
              "type": "info"
            }
          }
        }
      },
      "weight": 2
    }
  ],
  "query": {
    "match_all": {}
  },
  "min_score": 4,
  "score_mode": "sum"
}
}
}

Upvotes: 0

Views: 165

Answers (1)

Pierre Mallet
Pierre Mallet

Reputation: 7221

You should wrap this in a elasticbuilder.requestBodySearch()

In your case

elasticbuilder.requestBodySearch().query(not_body)

should do the job

Upvotes: 1

Related Questions