eubinecto / youtora

Search YouTube videos like you search books
4 stars 0 forks source link

search api #164

Closed eubinecto closed 4 years ago

eubinecto commented 4 years ago

why?

to isolate frontend from backend logics.

how?

~get use of django api framework?~ just use django views for that.

eubinecto commented 4 years ago

How: return JSON response from views?

There is JSONResponse object for that.

def my_view(request):

# do something with the your data
data = {}

# just return a JsonResponse
return JsonResponse(data)
eubinecto commented 4 years ago

How: where should I put the query logics? & how should I structure it?

location:

search
  - queries.py

design paradigm:

eubinecto commented 4 years ago

Having problems with referencing dotted fields

the error

  File "/Users/eubin/Desktop/Projects/Big/youtora/ytrenv/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 269, in perform_request
    self._raise_error(response.status, raw_data)
  File "/Users/eubin/Desktop/Projects/Big/youtora/ytrenv/lib/python3.8/site-packages/elasticsearch/connection/base.py", line 300, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', "object mapping [caption] can't be changed from non-nested to nested")

init을 하지 않고 그냥 저장을 한다음에, 다시 init을 시도하면 위와 같은 에러가 뜬다.

이걸 어떻게 해결해야하나? Nested field라면 reference를 어떻게 해야하지?

nested field, non-nested field 차이

curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}
'
curl -X PUT "localhost:9200/my-index-000001/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}
'
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "Smith" }} 
          ]
        }
      }
    }
  }
}
'
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "White" }} 
          ]
        }
      },
      "inner_hits": { 
        "highlight": {
          "fields": {
            "user.first": {}
          }
        }
      }
    }
  }
}
'
eubinecto commented 4 years ago

문제해결