우선 @karmi 및 @Zach에 동의합니다. 게시물 일치와 관련된 의미를 파악하는 것이 중요합니다. 간단히하기 위해 일치하는 게시물에 어딘가에 "개"라는 단어가 있다고 가정하고 태그 분석기를 사용하지 않고 태그를 매칭하여 재미있는 정보를 제공합니다.
귀하의 질문을 정확하게 이해하면 관련 게시물의 수를 기준으로 사용자를 주문하고 싶습니다. 관련 게시물을 찾고 사용자 쿼리에이 정보를 사용하려면 게시물을 검색해야합니다. 게시물이 별도로 색인화 된 경우에만 가능하며, 이는 게시물이 하위 문서 또는 중첩 필드 여야 함을 의미합니다.
게시물 자식 문서는 가정하면, 우리는이 같은 데이터를 프로토 타입 수 :
curl -XPOST 'http://localhost:9200/test-idx' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings" : {
"user" : {
"_source" : { "enabled" : true },
"properties" : {
"full_name": { "type": "string" },
"username": { "type": "string" }
}
},
"post" : {
"_parent" : {
"type" : "user"
},
"properties" : {
"title": { "type": "string"},
"tags": { "type": "string", "boost": 10}
}
}
}
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/user/1' -d '{
"full_name": "First User",
"username": "firstymcfirsterton"
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/user/2' -d '{
"full_name": "Second User",
"username": "seconddude"
}' && echo
#Posts of the first user
curl -XPUT 'http://localhost:9200/test-idx/post/1?parent=1' -d '{
"title": "Puppies are Awesome",
"tags": ["dog house", "dog supplies", "dogs", "doggies", "hot dogs", "dog lovers", "dog"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/2?parent=1' -d '{
"title": "Cats are Awesome too",
"tags": ["cat", "cat supplies", "cats"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/3?parent=1' -d '{
"title": "One fine day with a woof and a purr",
"tags": ["catdog", "cartoons"]
}' && echo
#Posts of the second user
curl -XPUT 'http://localhost:9200/test-idx/post/4?parent=2' -d '{
"title": "Dogs are the best",
"tags": ["dog supperiority", "dog"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/5?parent=2' -d '{
"title": "Why dogs eat?",
"tags": ["dog diet", "canines"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/6?parent=2' -d '{
"title": "Who let the dogs out?",
"tags": ["dogs", "terrible music"]
}' && echo
curl -XPOST 'http://localhost:9200/test-idx/_refresh' && echo
우리는 Top Children Query를 사용하여이 데이터를 조회 할 수 있습니다. 때문에 일치하는 태그에서 오는 엄청난 부스트 요소의 우선 첫 번째 사용자를 반환합니다
curl 'http://localhost:9200/test-idx/user/_search?pretty=true' -d '{
"query": {
"top_children" : {
"type": "post",
"query" : {
"bool" : {
"should": [
{ "text" : { "title" : "dog" } },
{ "text" : { "tags" : "dog" } }
]
}
},
"score" : "sum"
}
}
}' && echo
이 쿼리 (또는 중첩 된 필드의 경우 우리는 Nested Query를 사용하여 유사한 결과를 얻을 수있다). 그래서, 그것은 당신이 원하는 것처럼 보이지 않을 수도 있지만 그것을 고정시키는 몇 가지 간단한 방법이 있습니다. 첫째, 태그 필드의 부스트 요인을 줄일 수 있습니다. 10은 여러 번 반복 될 수있는 분야에서 특히 큰 요소입니다. 또는 일치하는 하위 조회수를 완전히 무시하고 상위 일치 하위 문서 수를 점수로 사용하도록 쿼리를 수정할 수 있습니다.
curl 'http://localhost:9200/test-idx/user/_search?pretty=true' -d '{
"query": {
"top_children" : {
"type": "post",
"query" : {
"constant_score" : {
"query" : {
"bool" : {
"should": [
{ "text" : { "title" : "dog" } },
{ "text" : { "tags" : "dog" } }
]
}
}
}
},
"score" : "sum"
}
}
}' && echo
몇 가지 생각과 질문. 어떻게 검색하고 있는지 게시 할 수 있습니까? 사용하는 쿼리 유형은 큰 차이를 만듭니다. 또한, 나는 "키워드"분석기를 사용하는 것이 좋은 생각이 아니라고 생각합니다. 전체 태그를 단일 용어로 변환합니다. 더 나은 해결책은 표준 분석기 + 사용자 정의 고유 필터 – Zach
@ thoughtpunch를 사용하는 것일 수 있습니다. 또한 100 % 질문을 이해하지 못하지만 적절한 "최상위"문서가 귀하의 예에서'post'가 될 것 같습니다. 그러면이 속성에 'user_id'가 포함됩니다. 나는 수색을 내기하고 전체 체제는 매우 쉬울 것입니다. – karmi