RediSearch / redisearch-py

RediSearch python client
https://redisearch.io
BSD 2-Clause "Simplified" License
221 stars 62 forks source link

[nightly] Unsuccessful search in JSON index (redisearch:2.2.0) #146

Closed clmnin closed 2 years ago

clmnin commented 2 years ago

I'm using the master branch and I have a version of redisearch-py working with redislabs/redisearch:2.2.0.

Here is the working version

Code ```python import rejson import redis import redisearch rj = rejson.Client(host='localhost', port=6379, decode_responses=True) # JSON.SET A:TLV . '{"iata": "TLV", "name": "Ben Gurion International Airport", "location": "34.8866997,32.01139832"}' obj = { 'iata': 'TLV', 'name': 'Ben Gurion International Airport', 'location': '34.8866997,32.01139832' } rj.jsonset('A:TLV', rejson.Path.rootPath(), obj) # JSON.SET A:SFO . '{"iata": "SFO", "name": "San Francisco International Airport", "location": "-122.375,37.6189995"}' obj = { 'iata': 'SFO', 'name': 'San Francisco International Airport', 'location': '-122.375,37.6189995' } rj.jsonset('A:SFO', rejson.Path.rootPath(), obj) # Get something print(f"{rj.jsonget('A:TLV', rejson.Path('.'))}") print(f"{rj.jsonget('A:SFO', rejson.Path('.'))}") # Create index # FT.CREATE idx:product ON HASH PREFIX 1 product: SCHEMA company TAG SORTABLE name TEXT SORTABLE description TEXT WEIGHT 0.5 internal_code TEXT WEIGHT 0.5 category TEXT SORTABLE # FT.CREATE airports ON JSON SCHEMA $.iata AS iata TAG SORTABLE $.iata AS iata_txt TEXT NOSTEM $.name AS name TEXT NOSTEM PHONETIC dm:en $.location AS location GEO definition = redisearch.IndexDefinition( prefix=['A:'], index_type=redisearch.client.IndexType.JSON) SCHEMA = ( redisearch.TextField("$.iata", as_name="iata"), redisearch.TextField("$.name", as_name="name") ) rc = redisearch.Client("airports") try: rc.info() except redis.ResponseError: # Index does not exist. We need to create it! rc.create_index(SCHEMA, definition=definition) res = rc.search("TLV") print(res) # Result{1 total, docs: [Document {'id': 'A:TLV', 'payload': None, 'json': '{"iata":"TLV","name":"Ben Gurion International Airport","location":"34.8866997,32.01139832"}'}]} ```

But when I change to a different JSON, the same code fails.

But this version does not work

  import rejson
  import redis
  import redisearch
  import json

  rj = rejson.Client(host='localhost', port=6379, decode_responses=True)

  one = "{\"id\": \"fd1e0952-108a-11ec-b4ad-2bdf65hg600b\", \"name\": \"Admin\", \"phone\": \"+1900700800\", \"company\": [{\"id\": \"fd1f89a8-108a-11ec-b4ad-2bdf65hg600b\"}, {\"id\": \"fe259b44-108a-11ec-b4ad-2bdf65hg600b\"}], \"is_active\": true}"
  two = "{\"id\": \"26a80b56-108b-11ec-b4ad-2bdf65hg600b\", \"name\": \"Not Admin\", \"phone\": \"+11900700801\", \"company\": [{\"id\": \"ef7de470-108a-11ec-b4ad-2bdf65hg600b\"}, {\"id\": \"ef5d49cc-108a-11ec-b4ad-2bdf65hg600b\"}], \"is_active\": true}"

  obj = json.loads(one)
  rj.jsonset('user:fd1e0952-108a-11ec-b4ad-2bdf65hg600b', rejson.Path.rootPath(), obj)

  obj = json.loads(two)
  rj.jsonset('user:26a80b56-108b-11ec-b4ad-2bdf65hg600b', rejson.Path.rootPath(), obj)

  # Get something
  print(f"{rj.jsonget('user:fd1e0952-108a-11ec-b4ad-2bdf65hg600b', rejson.Path('.phone'))}")
  print(f"{rj.jsonget('user:26a80b56-108b-11ec-b4ad-2bdf65hg600b', rejson.Path('.phone'))}")

  # Create index

  definition = redisearch.IndexDefinition(
      prefix=['user:'], index_type=redisearch.client.IndexType.JSON)

  SCHEMA = (
      redisearch.TextField("$.phone", as_name="phone"),
      redisearch.TextField("$.company", as_name="company")
  )
  rc = redisearch.Client("idx:user")

  try:
      rc.info()
  except redis.ResponseError:
      # Index does not exist. We need to create it!
      rc.create_index(SCHEMA, definition=definition)
  res = rc.search("+1900700800")
  print(res)
  # Result{0 total, docs: []}

My dificulty might be in properly creating an index. Could you please help me under where the error is?

PS: I'm reading/leaning the code and decided to check out a few things on my own. This is for personal usage and not for production.

clmnin commented 2 years ago

[Update] If I don't index company and instead index name the search works.

Question

  1. Can we not index nested objects (in this case List[Dict]<company>) as strings?
clmnin commented 2 years ago

A similar question was answered on

And so I'll be closing this issue.