Pregunta

I'm experimenting with ElasticSearch. I'm having problems with querying nested objects.

My mapping:

curl -X GET http://localhost:9200/testt/resource/_mapping?pretty

{
    "resource": {
        "properties": {
            "bib": {
                "type": "nested",
                "properties": {
                    "IssueDate": {
                        "type": "date",
                        "format": "dateOptionalTime"
                    },
                    "Title": {
                        "type": "string"
                    }
                }
            },
            "name": {
                "type": "string"
            }
        }
    }
}

I have one indexed resource:

curl -X GET http://localhost:9200/testt/resource/_search?pretty

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "testt",
                "_type": "resource",
                "_id": "1234",
                "_score": 1.0,
                "_source": {
                    "name": "SSS",
                    "bib": {
                        "Title": "XSD",
                        "IssueDate": "2012-12-19"
                    }
                }
            }
        ]
    }
}

curl -X GET http://localhost:9200/testt/resource/1234?pretty

{
    "_index": "testt",
    "_type": "resource",
    "_id": "1234",
    "_version": 1,
    "exists": true,
    "_source": {
        "name": "SSS",
        "bib": {
            "Title": "XSD",
            "IssueDate": "2012-12-19"
        }
    }
}

Yet I cannot find it using query request:

{
    "query": {
        "nested": {
            "path": "bib",
            "query": {
                "query_string": {
                    "query": "XSD"
                }
            }
        }
    }
}

Search: curl -X GET http://localhost:9200/testt/resource/_search?pretty -d '{ "query" : { "nested" : {"path" : "bib", "query" : { "query_string" : {"query" : "XSD"} } } } }'

{
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "hits" : {
        "total" : 0,
        "max_score" : null,
        "hits" : [ ]
    }
}

My question is: how can I use nested query to find my object? I'm interested in objects that have nested object bib that contains word XSD. Object 1234 clearly contains XSD, but I cannot find it. Can you tell me if my query is OK? What's wrong with it?

¿Fue útil?

Solución

The query_string doesn't support it, but if you can parse the query yourself, you can use multi_match query and do something like this:

{
    "query": {
        "nested": {
            "path": "bib",
            "query": {
                "multi_match": {
                    "query": "XSD",
                    "fields": ["bib.*"]
                }
            }
        }
    }
}

A possible problem with this solution is that if you have any numeric fields in the nested document, you would need to exclude them from the list of fields. It can be achieved by adding prefixes to fields names. For example, you can rename all string fields to start with s_, in this case you can choose all string fields by using "fields": ["bib.s_*"].

Another possible solution is to use parent's _all fields. You can exclude all parent's fields from the _all and use _all exclusively for the nested fields. All nested fields are included into parent's _all field by default.

Otros consejos

You need to specify the default field in your query_string query:

curl -XGET localhost:9200/testt/resource/_search -d '{ 
    "query": {
        "nested" : {
            "path" : "bib",
            "score_mode" : "avg",
            "query" : {
                "query_string" : {
                    "fields" : ["Title"],
                    "query" : "XSD"
                }
            }
        }
    }
}';
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top