Home > database >  Elasticsearch Query NOT searching in the specified fields
Elasticsearch Query NOT searching in the specified fields

Time:10-11

I am struggling with an elasticsearch query. In the fields option, we have specified '*' which means it should look in all fields as well as given the higher weights to a few fields. But it isn't working as it should. This query was written by my colleague, it'd be great if you could explain it as well as point out the solution. Here's my query:

{
  "query": {
    "bool": {
      "must": [
        {
          "simple_query_string": {
            "query": "Atoms for Peace",
            "default_operator": "AND",
            "flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
            "fields": [
              "*",
              "systemNumber^5",
              "global_search",
              "objectType^2",
              "partTypes.text",
              "partTypes.id",
              "gs_am_people^2",
              "gs_am_person^2",
              "gs_am_org^2",
              "gs_title^2",
              "_currentLocation.displayName",
              "briefDescription",
              "physicalDescription",
              "summaryDescription",
              "_flatPersonsNameId",
              "_flatPeoplesNameId",
              "_flatOrganisationsNameId",
              "_primaryDate",
              "_primaryDateEarliest",
              "_primaryDateLatest"
            ]
          }
        }
      ]
    }
  }


CodePudding user response:

Your query is fine but it will not work on field with "nested" data type.

From doc

Searching across all eligible fields does not include nested documents. Use a nested query to search those documents.

You need to use nested query

{
  "query": {
    "bool": {
      "minimum_should_match": 1, 
      "should": [
        {
          "simple_query_string": {
            "query": "Atoms for Peace",
            "default_operator": "AND",
            "flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
            "fields": [
              "*",
              "systemNumber^5",
              "global_search",
              "objectType^2",
              "partTypes.text",
              "partTypes.id",
              "gs_am_people^2",
              "gs_am_person^2",
              "gs_am_org^2",
              "gs_title^2",
              "_currentLocation.displayName",
              "briefDescription",
              "physicalDescription",
              "summaryDescription",
              "_flatPersonsNameId",
              "_flatPeoplesNameId",
              "_flatOrganisationsNameId",
              "_primaryDate",
              "_primaryDateEarliest",
              "_primaryDateLatest"
            ]
          }
        },
        {
          "nested": {
            "path": "record",
            "query": {
              "simple_query_string": {
                "query": "Atoms for Peace",
                "default_operator": "AND",
                "flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
                "fields": [
                  "*"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

  • Related