How to search query in main and nested doc along with some filters in elasticsearch


(Amit Gupta) #1

I have the following mapping for colleges in elasticsearch:

{
  "properties": {
    "name": {
      "type": "text"
    },
    "city": {
      "type": "keyword"
    },
    "programs": {
      "type": "nested",
      "properties": {
        "award": {
          "type": "text"
        },
        "level": {
          "type": "keyword"
        }
      }
    }
  }
}

I want to run search on this index as:

  • search query on name and programs.award
  • filter query on city and programs.level

We have a filter box for city, level and an open text field in the UI.

I am using the following query right now:

{
	"from": 0,
	"size": 10,
	"query": {
		"bool": {
			"must": [
				{
					"terms": {
						"city": [
							"Some City"
						]
					}
				},
				{
					"nested": {
						"path": "programs",
						"inner_hits": {
							"size": 10
						},
						"query": {
							"bool": {
								"must": [
									{
										"terms": {
											"programs.level": ["Under Graduate"]
										}
									}
								],
								"should": [
									{
										"simple_query_string": {
											"query": "engineering~",
											"fields": ["programs.award"]
										}
									}
								]
							}
						}
					}
				}
			],
			"should": [
				{
					"simple_query_string": {
						"query": "engineering",
						"fields": ["name"]
					}
				}
			]
		}
	}
}

For terms filter it's working fine but for simple_query_string it's not working as expected.

I want an OR query search between name and programs.award.

I am not sure what I am doing wrong here if anyone can help.

Note: If it matches the name property then it should return all programs in paginated form.


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.