-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
185 lines (150 loc) · 4.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from typing import List, Optional
import uvicorn
from elasticsearch import AsyncElasticsearch
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
es = AsyncElasticsearch(hosts="http://localhost:9200")
class Tweet(BaseModel):
message: str
user: str
client: str
retweeted: bool
source: str
urls: List[str]
class OptionalTweet(BaseModel):
message: Optional[str]
user: Optional[str]
client: Optional[str]
retweeted: Optional[bool]
source: Optional[str]
urls: Optional[List[str]]
@app.get("/")
async def read_all():
return await es.search(index="tweets")
@app.get("/{id}")
async def read_one(id: str):
return await es.get(index="tweets", id=id)
@app.delete("/{id}")
async def delete_one(id: str):
return await es.delete(index="tweets", id=id)
@app.post("/create")
async def create_one(tweet: Tweet):
return await es.index(index="tweets", document=tweet.json())
@app.put("/{id}")
async def update_one(id: str, tweet: OptionalTweet):
return await es.update(index="tweets", id=id, doc=tweet.dict(exclude_unset=True))
class SearchRequest(BaseModel):
query: str
publication: Optional[str]
date_filter: Optional[dict]
author: Optional[str]
page: int = 1
def generate_fts_clause(request: SearchRequest) -> dict:
return {
"bool": {
"should": [
{
"multi_match": {
"fields": [
"title", "title.english"
],
"boost": 50,
"type": "phrase",
"query": request.query
}
},
{
"multi_match": {
"fields": [
"content", "content.english"
],
"boost": 2,
"slop": 4,
"type": "phrase",
"query": request.query
}
},
{
"multi_match": {
"fields": [
"content", "content.english", "content", "content.english"
],
"query": request.query
}
}
]
}
}
def generate_filters(request: SearchRequest) -> List[dict]:
filters = []
if request.author:
filters.append({
"match": {
"author": request.author
}
})
if request.date_filter:
filters.append({
"range": {
"date": request.date_filter
}
})
return filters
def calc_paging(page: int):
return (page - 1) * 10
@app.post("/search")
async def search(request: SearchRequest):
highlight, query = await build_query(request)
aggs = {
"authors": {
"terms": {
"field": "authors.keyword",
"size": 50
}
}
}
suggest = {
"did_you_mean": {
"text": request.query,
"phrase": {
"field": "title"
}
}
}
return await es.search(index="books", query=query, _source=["title"], highlight=highlight,
from_=calc_paging(request.page), aggregations=aggs, suggest=suggest)
async def build_query(request):
fts_clause = generate_fts_clause(request)
filter_clauses = generate_filters(request)
query = {
"bool": {
"must": fts_clause,
"filter": filter_clauses
}
}
highlight = {
"fields": {
"title": {}
}
}
return highlight, query
@app.post("/author_search")
async def author_search(request: SearchRequest):
highlight, query = await build_query(request)
collapse = {
"field": "authors.keyword",
"inner_hits": {
"name": "books_By_author",
"_source": ["title"],
"size": 3
}
}
return await es.search(index="books", query=query, _source=False, highlight=highlight,
from_=calc_paging(request.page), collapse=collapse)
if __name__ == "__main__":
# Run uvicorn
uvicorn.run(
"main:app",
reload=True,
)