【问题标题】:python elasticsearch dsl search for field with white spacepython elasticsearch dsl搜索带有空格的字段
【发布时间】:2017-03-28 06:32:34
【问题描述】:

案例 1 有效,但案例 2 无效,这是由于空白。 这是一个已经创建了一段时间并被许多人使用的索引,所以我的搜索有问题而不是索引。也许它与标记器或分析器有关,但不确定是什么/如何

case 2 由于“san francisco”中的空格而没有返回任何数据

案例一

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

【问题讨论】:

  • 您可以使用 es=Elasticsearch() es.indices.get_mapping("my_index") 检查您的映射并在此处发布吗?我认为这会有所帮助。
  • 刚刚添加。猜你正在寻找这部分? u'city': {u'type': u'string', u'store': True}
  • 从映射看来,默认分析器 (elastic.co/guide/en/elasticsearch/reference/current/…) 用于此字段,它正在标记字段值。您正在搜索“术语”,它正在搜索确切的术语。如果你使用“匹配”,你应该会得到结果。
  • 做到了????????
  • 太棒了!请接受我的回答。

标签: python elasticsearch elasticsearch-dsl elasticsearch-dsl-py


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2021-07-08
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多