alyssa moon
alyssa moon

Reputation: 35

Elasticsearch search across multiple fields including nested

i have this mapping on my elasticsearch index:

{
  "mappings": {
    "properties": {
      "title": {
        "type": "keyword"
      },
      "description": {
        "type": "text"
      },
      "content": {
        "type": "text"
      },
      "author": {
        "type": "keyword"
      },
      "completionTimeMinutes": {
        "type": "integer"
      },
      "views": {
        "type": "integer"
      },
      "questions": {
        "type": "nested",
        "properties": {
          "question": {
            "type": "keyword"
          },
          "answers": {
            "type": "keyword"
          },
          "rightAnswer": {
            "type": "integer"
          }
        }
      }
    }
  }
}

And i have some problems with querying from multiple fields. In my case i want to search across title, description, content, questions.question, questions.answer. My query that doesn't work looks like this:

{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "text to search i put here",
                    "type": "cross_fields",
                    "fields": [
                        "title",
                        "description",
                        "content",
                        "questions.question",
                        "questions.answers"
                    ],
                    "slop": 1
                }
            }
        }
    }
}

I also tried {"query": { "nested": { "path": "questions", ......, where ...... is above query, but if so, i cant search in title, description, content. So please tell me how to query from multiple nested levels.

Upvotes: 0

Views: 1340

Answers (1)

Bhavya
Bhavya

Reputation: 16172

You need to combine nested and non-nested search queries. Refer ES official documentation, to know more about bool queries.

Adding a working example with index data, search query, and search result

Index Data:

{
    "title": "a",
    "description": "a",
    "content": "b",
    "questions": [
        {
            "question": "c",
            "answers": "c"
        }
    ]
}

Search Query:

{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "a",
                        "type": "cross_fields",
                        "fields": [
                            "title",
                            "content"
                        ],
                        "slop": 1
                    }
                },
                {
                    "nested": {
                        "path": "questions",
                        "query": {
                            "multi_match": {
                                "query": "c",
                                "type": "cross_fields",
                                "fields": [
                                    "title",
                                    "content",
                                    "questions.question",
                                    "questions.answers"
                                ],
                                "slop": 1
                            }
                        }
                    }
                }
            ],
            "minimum_should_match":1
        }
    }
}

Search Result:

"hits": [
      {
        "_index": "stof_64155263",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "a",
          "description": "a",
          "content": "b",
          "questions": [
            {
              "question": "c",
              "answers": "c"
            }
          ]
        }
      }
    ]

Upvotes: 0

Related Questions