Search code examples
pythonelasticsearchelasticsearch-dslelasticsearch-dsl-py

python elasticsearch dsl search for field with white space


case 1 works but not case 2 and this is due to the white space. This is an index that has been created for sometime and used by many folks so is something wrong with my search not with the index. Perhaps it has something to do with the tokenizer or analyser but not sure what/how

case 2 returns no data due to the whitespace in "san francisco"

case 1

s = Search(using=client, index = "my_index")
s = s.query("nested",
    path="party.partyAddress",
    query=Q("term", party__partyAddress__city="fremont")
)

print (s.execute)

case 2

s = Search(using=client, index = "my_index")
s = s.query("nested",
    path="party.partyAddress",
    query=Q("term", party__partyAddress__city="san francisco")
)
s.execute
print (s.execute)

Adding mapping as requested

{
  u'policiesrel3': {
    u'mappings': {
      u'data': {
        u'properties': {
          u'prodTypeCode': {
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'contractAddress': {
            u'type': u'nested',
            u'properties': {
              u'city': {
                u'type': u'string',
                u'store': True
              },
              u'addressType': {
                u'type': u'string',
                u'store': True
              },
              u'address1': {
                u'type': u'string',
                u'store': True
              },
              u'address2': {
                u'type': u'string',
                u'store': True
              },
              u'address3': {
                u'type': u'string',
                u'store': True
              },
              u'zipCode': {
                u'type': u'string',
                u'store': True
              },
              u'pinLatLongLoc': {
                u'properties': {
                  u'location': {
                    u'type': u'nested'
                  }
                }
              },
              u'state': {
                u'fields': {
                  u'raw': {
                    u'index': u'not_analyzed',
                    u'type': u'string'
                  }
                },
                u'type': u'string',
                u'store': True
              }
            }
          },
          u'agencyCode': {
            u'include_in_all': True,
            u'type': u'string',
            u'store': True
          },
          u'docType': {
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'policyNumber_suggest': {
            u'preserve_separators': True,
            u'analyzer': u'simple',
            u'payloads': False,
            u'max_input_length': 50,
            u'preserve_position_increments': True,
            u'type': u'completion'
          },
          u'mdmContractTransactionTimestamp': {
            u'type': u'string'
          },
          u'productCode': {
            u'include_in_all': True,
            u'type': u'string',
            u'store': True
          },
          u'policyLoadedToEStimeStamp': {
            u'type': u'string'
          },
          u'memType': {
            u'index': u'not_analyzed',
            u'type': u'string',
            u'store': True
          },
          u'termExpirationDate': {
            u'include_in_all': True,
            u'index': u'analyzed',
            u'type': u'date',
            u'store': True,
            u'format': u'yyyy/MM/dd'
          },
          u'policyStatus': {
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'party': {
            u'include_in_parent': True,
            u'type': u'nested',
            u'properties': {
              u'maritalStatus': {
                u'type': u'string',
                u'store': True
              },
              u'suffix': {
                u'index': u'not_analyzed',
                u'type': u'string',
                u'store': True
              },
              u'prefix': {
                u'index': u'not_analyzed',
                u'type': u'string',
                u'store': True
              },
              u'memNum': {
                u'type': u'string'
              },
              u'lastName_suggest': {
                u'preserve_separators': True,
                u'analyzer': u'simple',
                u'payloads': False,
                u'max_input_length': 50,
                u'preserve_position_increments': True,
                u'type': u'completion'
              },
              u'partyTransactionTimeStamp': {
                u'include_in_all': True,
                u'type': u'string',
                u'store': True
              },
              u'memDt': {
                u'type': u'date',
                u'format': u'yyyy/MM/dd HH:mm:ss||yyyy/MM/dd'
              },
              u'customerStatus': {
                u'type': u'string',
                u'store': True
              },
              u'middleName': {
                u'type': u'string',
                u'store': True
              },
              u'empInd': {
                u'type': u'string',
                u'store': True
              },
              u'roleList': {
                u'include_in_parent': True,
                u'type': u'nested',
                u'properties': {
                  u'roleEndDate': {
                    u'type': u'date',
                    u'format': u'yyyy/MM/dd HH:mm:ss||yyyy/MM/dd'
                  },
                  u'partyRoleType': {
                    u'index': u'not_analyzed',
                    u'type': u'string',
                    u'store': True
                  },
                  u'sourceId': {
                    u'include_in_all': True,
                    u'type': u'string',
                    u'store': True
                  },
                  u'roleStatus': {
                    u'type': u'string'
                  },
                  u'sourceID': {
                    u'type': u'string'
                  }
                }
              },
              u'pegaAccount': {
                u'type': u'string'
              },
              u'email': {
                u'type': u'string'
              },
              u'partyAddress': {
                u'type': u'nested',
                u'properties': {
                  u'city': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'addressType': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'address1': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'address2': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'address3': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'zipCode': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'pinLatLongLoc': {
                    u'properties': {
                      u'location': {
                        u'type': u'geo_point'
                      }
                    }
                  },
                  u'state': {
                    u'fields': {
                      u'raw': {
                        u'index': u'not_analyzed',
                        u'type': u'string'
                      }
                    },
                    u'type': u'string',
                    u'store': True
                  }
                }
              },
              u'dobYear': {
                u'type': u'string',
                u'store': True
              },
              u'registrationId': {
                u'type': u'string'
              },
              u'fullName': {
                u'search_analyzer': u'typeahead_search_ngram',
                u'index_analyzer': u'typeahead_index_ngram',
                u'type': u'string'
              },
              u'partyIDs': {
                u'dynamic': u'false',
                u'type': u'nested',
                u'properties': {
                  u'idType': {
                    u'index': u'not_analyzed',
                    u'type': u'string',
                    u'store': True
                  },
                  u'details': {
                    u'type': u'nested',
                    u'properties': {
                      u'startDate': {
                        u'type': u'date',
                        u'format': u'yyyy/MM/dd'
                      },
                      u'endDate': {
                        u'type': u'date',
                        u'format': u'yyyy/MM/dd'
                      },
                      u'number': {
                        u'type': u'string',
                        u'store': True
                      },
                      u'sourceValue': {
                        u'index': u'not_analyzed',
                        u'type': u'string',
                        u'store': True
                      }
                    }
                  }
                }
              },
              u'firstName_suggest': {
                u'preserve_separators': True,
                u'analyzer': u'simple',
                u'payloads': False,
                u'max_input_length': 50,
                u'preserve_position_increments': True,
                u'type': u'completion'
              },
              u'firstName': {
                u'include_in_all': True,
                u'fields': {
                  u'autocomplete': {
                    u'type': u'string',
                    u'analyzer': u'simple'
                  },
                  u'raw': {
                    u'index': u'not_analyzed',
                    u'type': u'string'
                  }
                },
                u'type': u'string',
                u'store': True
              },
              u'dob': {
                u'type': u'date',
                u'store': True,
                u'format': u'yyyy/MM/dd'
              },
              u'gender': {
                u'type': u'string',
                u'store': True
              },
              u'mdmId': {
                u'index': u'not_analyzed',
                u'type': u'string',
                u'store': True
              },
              u'driversLicenseNumber': {
                u'type': u'string'
              },
              u'SSN': {
                u'type': u'string'
              },
              u'phoneList': {
                u'type': u'nested',
                u'properties': {
                  u'phoneExtension': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'phoneFullNumber': {
                    u'index': u'not_analyzed',
                    u'type': u'string',
                    u'store': True
                  },
                  u'phoneExchange': {
                    u'type': u'string',
                    u'store': True
                  },
                  u'phoneNumber': {
                    u'index': u'not_analyzed',
                    u'type': u'string',
                    u'store': True
                  },
                  u'phoneType': {
                    u'index': u'not_analyzed',
                    u'type': u'string',
                    u'store': True
                  },
                  u'phoneAreaCode': {
                    u'type': u'string',
                    u'store': True
                  }
                }
              },
              u'lastName': {
                u'include_in_all': True,
                u'fields': {
                  u'autocomplete': {
                    u'type': u'string',
                    u'analyzer': u'simple'
                  },
                  u'raw': {
                    u'index': u'not_analyzed',
                    u'type': u'string'
                  }
                },
                u'type': u'string',
                u'store': True
              }
            }
          },
          u'ubiFlag': {
            u'index': u'not_analyzed',
            u'type': u'string',
            u'store': True
          },
          u'policyType': {
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'policyNumberPartial': {
            u'include_in_all': True,
            u'fields': {
              u'autocomplete': {
                u'type': u'string',
                u'analyzer': u'simple'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'sourceSystem': {
            u'include_in_all': True,
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'contractTransactionTimeStamp': {
            u'include_in_all': True,
            u'type': u'string',
            u'store': True
          },
          u'policyInceptionDate': {
            u'include_in_all': True,
            u'index': u'analyzed',
            u'type': u'date',
            u'store': True,
            u'format': u'yyyy/MM/dd'
          },
          u'policyNumber': {
            u'include_in_all': True,
            u'fields': {
              u'autocomplete': {
                u'search_analyzer': u'typeahead_search_ngram',
                u'index_analyzer': u'typeahead_index_ngram',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'termEffectiveDate': {
            u'include_in_all': True,
            u'index': u'analyzed',
            u'type': u'date',
            u'store': True,
            u'format': u'yyyy/MM/dd'
          },
          u'riskState': {
            u'include_in_all': True,
            u'fields': {
              u'raw': {
                u'index': u'not_analyzed',
                u'type': u'string'
              }
            },
            u'type': u'string',
            u'store': True
          },
          u'mdmPartyTransactionTimestamp': {
            u'type': u'string'
          },
          u'agentid': {
            u'include_in_all': True,
            u'type': u'string',
            u'store': True
          }
        }
      }
    }
  }
}

Solution

  • From the mapping it seems that the default analyzer (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html) is used for this field, which is tokenizing the field value. You are searching with term, which is searching for the exact term. If you use match, you should get results.