jurismarches / luqum

A lucene query parser generating ElasticSearch queries and more !
Other
188 stars 40 forks source link

Multi-level nesting fields problem #15

Closed synweap15 closed 7 years ago

synweap15 commented 7 years ago

Hello!

First of all I'd like to thank the authors of this amazing library for all the effort - this library really helps when it comes to querying nested fields with ES' Query String Query.

I've found some issue concerning the parser when it comes to parsing multi-level nested fields (nested fields within nested fields).

Here's my definition of ElasticsearchQueryBuilder:

es_builder = ElasticsearchQueryBuilder(nested_fields={
        "study_units": {
            "country": {"id": {}, "name": {}}
    })

The query looks like this: study_units.country.name:italy

The produced output looks like this:

{
  'nested': {
    'query': {
      'nested': {
        'query': {
          'match': {
            'study_units.country.name': {
              'type': 'phrase',
              'query': 'italy',
              'zero_terms_query': 'none'
            }
          }
        },
        'path': 'study_units.country'
      }
    },
    'path': 'name'
  }
}

I think that the most outer path key should have the value of study_units instead of name.

My question - is this a problem with my definition of nested fields, or something is off here?

synweap15 commented 7 years ago

Figured it out - obviously I made a mistake taking some assumptions concerning Lucene syntax for nested fields. Did some reading and the query should be formulated as follows: study_units:(country:(name:italy)) - works like a charm!

ahivert commented 7 years ago

You could write the query like this study_units:country:name:italy too :)

synweap15 commented 7 years ago

Right, thanks @ahivert!

Another question: I have defined a few fields as Object types instead of Nested using elasticsearch-dsl, so they can be searched without explicitly specifying fields. The problem is that when I search using eg. country.name:Italy (not nested, just flattened, looks correct in Kibana), it's still treated as nested by Luqum - and then depending whether it was defined in nested_fields or not, I get RequestError for Elasticsearch or NestedSearchFieldException from _recurse_nested_fields.

Any hints?

alexgarel commented 7 years ago

Hi @synweap15, indeed we did not have Object in mind when we decide to deal with nested field. Could you file a new ticket, possibly with expected behaviour ?

For now I close this ticket.

synweap15 commented 7 years ago

Sure, I'll open a new issue. Thanks!