Reactivesearch: customQuery/ defaultQuery failing without `query` key

Created on 29 Apr 2020  路  6Comments  路  Source: appbaseio/reactivesearch

Affected Projects
React

Library Version: x.y.z
3.8.0

Describe the bug
customQuery and defaultQuery are returning Unknown key for a START_OBJECT. This was noticed in #1388 when writing a query custom query with a nested attribute. Even custom queries without nested attributes fail.

To Reproduce
https://codesandbox.io/s/broken-ssr-nested-example-9k64j?file=/pages/index.js

Steps to reproduce the behavior:

Try running the example then refresh the sandbox browser. You'll see a failed _msearch query in the network tab with the reason: Unknown key for a START_OBJECT in [nested].

Here is the generated query:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "genre_nested",
                  "query": {
                    "term": {
                      "genre_nested.genre": "drama"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10,
  "_source": {
    "includes": [
      "*"
    ],
    "excludes": []
  },
  "from": 0,
  "nested": {
    "path": "genre_nested",
    "query": {
      "term": {
        "genre_nested.genre": "drama"
      }
    }
  }
}

the custom query seems to be separated from the main query as well.

Expected behavior
customQuery / defaultQuery properly embed into the generated query.

bug

Most helpful comment

@carlopascual have fixed the issue. Also, you need to modify your queries to:

@@ -11,11 +11,13 @@
    componentId: 'Genres',
    dataField: 'title',
    customQuery: value => ({
-       nested: {
-           path: 'genre_nested',
-           query: {
-               term: {
-                   'genre_nested.genre': value,
+       query: {
+           nested: {
+               path: 'genre_nested',
+               query: {
+                   term: {
+                       'genre_nested.genre': value || 'Drama',
+                   },
                },
            },
        },
@@ -31,11 +33,13 @@
    URLParams: true,
    className: 'result-list-container',
    defaultQuery: value => ({
-       nested: {
-           path: 'genre_nested',
-           query: {
-               term: {
-                   'genre_nested.genre': value,
+       query: {
+           nested: {
+               path: 'genre_nested',
+               query: {
+                   term: {
+                       'genre_nested.genre': value || 'Drama',
+                   },
                },
            },
        },

Note 'genre_nested.genre': value || 'Drama', otherwise it will throw error

All 6 comments

@carlopascual can you once check if your query is correct? I was inspecting it and found that the fieldname cannot be empty.

To summarize, nested queries are not getting combined properly. We need to solve this.

Here's an example of it failing with a non-nested query.

https://codesandbox.io/s/broken-default-query-example-g5uu9?file=/pages/index.js

defaultQuery on reactivelist:

 defaultQuery: value => ({
    match: {
      genre: "drama"
    }
  }),

result: "Unknown key for a START_OBJECT in [match]."
query generated:

{"preference":"SearchResult"}
{
  "query": {
    "match_all": {}
  },
  "size": 10,
  "_source": {
    "includes": [
      "*"
    ],
    "excludes": []
  },
  "from": 0,
  "match": {
    "genre": "drama"
  }
}

Here's an example of it failing with a non-nested query.

https://codesandbox.io/s/broken-default-query-example-g5uu9?file=/pages/index.js

defaultQuery on reactivelist:

 defaultQuery: value => ({
    match: {
      genre: "drama"
    }
  }),

result: "Unknown key for a START_OBJECT in [match]."
query generated:

{"preference":"SearchResult"}
{
  "query": {
    "match_all": {}
  },
  "size": 10,
  "_source": {
    "includes": [
      "*"
    ],
    "excludes": []
  },
  "from": 0,
  "match": {
    "genre": "drama"
  }
}

@carlopascual You can assume that every defaultQuery and customQuery should have a query key. So in your case the defaultQuery should be:

defaultQuery: value => ({
    query: {
      match: {
        genre: "drama"
      }
    }
  }),

Also, customQuery and defaultQuery not working when query is nested is a different issue, which we're looking into.

@carlopascual have fixed the issue. Also, you need to modify your queries to:

@@ -11,11 +11,13 @@
    componentId: 'Genres',
    dataField: 'title',
    customQuery: value => ({
-       nested: {
-           path: 'genre_nested',
-           query: {
-               term: {
-                   'genre_nested.genre': value,
+       query: {
+           nested: {
+               path: 'genre_nested',
+               query: {
+                   term: {
+                       'genre_nested.genre': value || 'Drama',
+                   },
                },
            },
        },
@@ -31,11 +33,13 @@
    URLParams: true,
    className: 'result-list-container',
    defaultQuery: value => ({
-       nested: {
-           path: 'genre_nested',
-           query: {
-               term: {
-                   'genre_nested.genre': value,
+       query: {
+           nested: {
+               path: 'genre_nested',
+               query: {
+                   term: {
+                       'genre_nested.genre': value || 'Drama',
+                   },
                },
            },
        },

Note 'genre_nested.genre': value || 'Drama', otherwise it will throw error

any update for this? :)

Was this page helpful?
0 / 5 - 0 ratings