Elastic Search 및 SQL 이외의 패러다임을 처음 사용했습니다. 나는 ES 튜토리얼을 따라 왔지만 일을 할 수없는 한 가지가있다.Elastic 검색에서 다중 레벨 중첩 필드 쿼리
다음 코드에서는 (ES와 상호 작용하기 위해 PyES을 사용합니다.) 중첩 필드 (제목)와 함께 다른 중첩 필드 (개념)가 포함 된 단일 문서를 만듭니다.
이query1 = {
"nested": {
"path": "subjects",
"score_mode": "avg",
"query": {
"bool": {
"must": [
{
"text": {"subjects.subjectname": "subject1"}
},
{
"range": {"subjects.subjectkey": {"gt": 1}}
}
]
}
}
}
}
results = conn.search(query=query1)
for r in results:
print r # as expected, it returns the entire document.
하지만 개념 중첩 된 필드를 기준으로 쿼리하는 방법을 알아낼 수 없습니다 :
from pyes import *
conn = ES('127.0.0.1:9200') # Use HTTP
# Delete and Create a new index.
conn.indices.delete_index("documents-index")
conn.create_index("documents-index")
# Create a single document.
document = {
"docid": 123456789,
"title": "This is the doc title.",
"description": "This is the doc description.",
"datepublished": 2005,
"author": ["Joe", "John", "Charles"],
"subjects": [{
"subjectname": 'subject1',
"subjectid": [210, 311, 1012, 784, 568],
"subjectkey": 2,
"concepts": [
{"name": "concept1", "score": 75},
{"name": "concept2", "score": 55}
]
},
{
"subjectname": 'subject2',
"subjectid": [111, 300, 141, 457, 748],
"subjectkey": 0,
"concepts": [
{"name": "concept3", "score": 88},
{"name": "concept4", "score": 55},
{"name": "concept5", "score": 66}
]
}],
}
# Define the nested elements.
mapping1 = {
'subjects': {
'type': 'nested'
}
}
mapping2 = {
'concepts': {
'type': 'nested'
}
}
conn.put_mapping("document", {'properties': mapping1}, ["documents-index"])
conn.put_mapping("subjects", {'properties': mapping2}, ["documents-index"])
# Insert document in 'documents-index' index.
conn.index(document, "documents-index", "document", 1)
# Refresh connection to make queries.
conn.refresh()
나는 주제 중첩 된 필드를 조회 할 수 있어요.
ES documentation는
멀티 레벨의 중첩이 자동으로 다른 중첩 내에 존재하는 경우 가 자동으로 (그리고 루트) 관련 중첩 수준에 맞게 내부 중첩 된 쿼리의 결과로, 지원 및 탐지되는 것을 의미한다 질문.
query2 = { "nested": { "path": "concepts", "score_mode": "avg", "query": { "bool": { "must": [ { "text": {"concepts.name": "concept1"} }, { "range": {"concepts.score": {"gt": 0}} } ] } } } }
0 결과를 반환
그래서, 나는 다음과 같은 형식의 쿼리를 작성하는 tryed.
누락 된 항목을 알아낼 수 없으며 두 단계의 중첩을 기반으로하는 쿼리의 예가 없습니다.
하나의 부울 조건 만 사용하기 때문에 더 정확한 쿼리 정의는 "query"를 사용하는 것입니다. { "text": { "subjects.concepts.name": "concept1"}} – JCJS