python elasticsearch dsl 搜索带白色的字段 space

python elasticsearch dsl search for field with white space

情况 1 有效但情况 2 无效,这是由于白色 space。 这是一个已经创建了一段时间并被许多人使用的索引,所以我的搜索有问题而不是索引。也许它与分词器或分析器有关但不确定 what/how

案例2 returns由于"san francisco"

中的白色space没有数据

案例 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)

案例 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)

按要求添加映射

{
  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
          }
        }
      }
    }
  }
}

从映射来看,默认分析器 (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html) 似乎用于此字段,它正在标记字段值。您正在使用 term 进行搜索,这是在搜索确切的字词。如果您使用 match,您应该会得到结果。