티스토리 뷰

결과 및 요약

  • 구버전에서의 percolator API 처럼 쓸수 있을것 같음
  • default_field 매핑 잡고 쿼리에 기본 값으로 넣어둬야 함
  • percolate search query 에서 query_string 쓸꺼면 bool query 로 말아두어야 함
  • 프로덕션 레벨에서 사용하기 위한 추가 검증 필요 사항
    • routing 적용 여부
    • projectName 필드 고정

배경

준비

xephysis@heejun con % curl localhost:9200 
{
  "name" : "heejun.local",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "u_Cqt2vDTmykoKYsFQDFEQ",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
    "build_date" : "2021-01-13T00:42:12.435326Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

가이드에 따른 최초 검증

percolate 에 사용할 인덱스 생성

  • 요청
curl -X PUT "localhost:9200/nelo2-percolate?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "message": {
        "type": "text"
      },
      "query": {
        "type": "percolator"
      }
    }
  }
}
'
  • 응답
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "nelo2-percolate"
}

percolate 에서 사용할 쿼리? 저장

  • 요청
curl -X PUT "localhost:9200/nelo2-percolate/_doc/1?refresh&pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "message": "xephysis"
    }
  }
}
'
  • 응답
    • 문서 아이디 1번으로 생성
{
  "_index" : "nelo2-percolate",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "forced_refresh" : true,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

등록된 문서가 잘 검색되는지 검증 (percolate API 기능 확인)

없는 요청

curl -X GET "localhost:9200/nelo2-percolate/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "message": "lolem ipsum"
      }
    }
  }
}
'
  • 응답
    • 기대한것처럼 아무것도 나오지 않음
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

있는 요청

  • 요청
curl -X GET "localhost:9200/nelo2-percolate/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "message": "xephysis test"
      }
    }
  }
}
'
  • 응답
    • 기대한것처럼 동작
{
  "took" : 28,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.13076457,
    "hits" : [
      {
        "_index" : "nelo2-percolate",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13076457,
        "_source" : {
          "query" : {
            "match" : {
              "message" : "xephysis"
            }
          }
        },
        "fields" : {
          "_percolator_document_slot" : [
            0
          ]
        }
      }
    ]
  }
}

percolate 에서 사용할 쿼리? 추가 저장

  • 요청
curl -X PUT "localhost:9200/nelo2-percolate/_doc/2?refresh&pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "message": "test"
    }
  }
}
'
  • 응답
    • 문서 아이디 2번으로 생성
{
  "_index" : "nelo2-percolate",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "result" : "created",
  "forced_refresh" : true,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}
  • 요청
curl -X GET "localhost:9200/nelo2-percolate/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "message": "xephysis test"
      }
    }
  }
}
'
  • 응답
    • 등록해 둔 두개 문서 모두 검색됨
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.13076457,
    "hits" : [
      {
        "_index" : "nelo2-percolate",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13076457,
        "_source" : {
          "query" : {
            "match" : {
              "message" : "xephysis"
            }
          }
        },
        "fields" : {
          "_percolator_document_slot" : [
            0
          ]
        }
      },
      {
        "_index" : "nelo2-percolate",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.13076457,
        "_source" : {
          "query" : {
            "match" : {
              "message" : "test"
            }
          }
        },
        "fields" : {
          "_percolator_document_slot" : [
            0
          ]
        }
      }
    ]
  }
}

중간정리

  • 기본 동작 확인
  • percolate api 는 구버전에서 사용되는 percolate query 와 응답 형상이 많이 다름
    • 기존 처럼 루씬 쿼리를 저장해 놓고 문서를 통째로 넘겨서 쓰는게 가능할지

예전 처럼 동작하는걸 의도 - 쿼리 등록

  • 최초 시도
curl --location --request PUT 'localhost:9200/nelo2-percolate/_doc/3?refresh&pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "query_string": {
        "query": "projectName:\"xep-test\" AND host:\"xep-local\" body:xep*",
        "lowercase_expanded_terms": false,
        "default_operator": "AND",
        "analyze_wildcard": true,
        "default_field": "body"
    }
  }
}'
  • 문제 발생
    • "reason": "[query_string] query does not support [lowercase_expanded_terms]",
      • 옵션이 현재버전에서는 없어졌으므로 제거
    • "reason": "No field mapping can be found for the field with name [body]",
      • 최초로 인덱스 등록하던 시점에 body에 대한 매핑에 없으므로 에러 발생 옵션 제거
    • "reason": "No field mapping can be found for the field with name [*]",
      • ????
      • query_string 이 안먹는건 아닐텐데
  • 매핑을 고치고 body 필드를 잡아두는걸로
curl --location --request PUT 'localhost:9200/nelo2-percolate/_mapping?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "properties": {
        "body": {
            "type": "text"
        },
        "message": {
            "type": "text"
        },
        "query": {
            "type": "percolator"
        }
    }
}'
  • 매핑이 등록되었으므로 쿼리 등록 재시도
  • 요청
curl --location --request PUT 'localhost:9200/nelo2-percolate/_doc/3?refresh&pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "query_string": {
        "query": "projectName:\"xep-test\" AND host:\"xep-local\" AND body:xep*",
        "default_operator": "AND",
        "analyze_wildcard": true,
        "default_field": "body"
    }
  }
}'
  • 응답
{
    "_index": "nelo2-percolate",
    "_type": "_doc",
    "_id": "3",
    "_version": 1,
    "result": "created",
    "forced_refresh": true,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}
  • 성공
    • default_field 가 없으면, [*] 를 대상으로 쿼리를 수행하려고 하는걸로 보이며 default_field 옵션이 필수로 보임

예전 처럼 동작하는걸 의도 - 쿼리 검색 요청

  • 요청
curl --location --request GET 'localhost:9200/nelo2-percolate/_search?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "body": "xephysis test",
        "projectName":"xep-test",
        "host":"xep-local",
        "logTime": 1625624607
      }
    }
  }
}
'
  • 응답
    • 기대했던 응답 결과가 나오지 않음
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}
  • 쿼리 수정
    • 최초
      • "query": "projectName:\"xep-test\" AND host:\"xep-local\" AND body:xep*",
    • 변경
      • "query": "projectName:\"xep-test\""
    • 결과
      • 동일
      • 등록된 쿼리로 문서를 검색하지 못하는 상황일지 반대로 검증

문서 저장 및 검색 재시도

  • 인덱스 생성 요청 및 결과
curl --location --request PUT 'localhost:9200/nelo2-log-20210707?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "mappings": {
        "properties": {
            "body": {
                "type": "text"
            }
        }
    }
}'
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "nelo2-log-20210707"
}
  • 문서 저장 요청 및 결과
curl --location --request POST 'localhost:9200/nelo2-log-20210707/_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
        "body": "xephysis test",
        "projectName":"xep-test",
        "host":"xep-local",
        "logTime": 1625624607
      }'

{
    "_index": "nelo2-log-20210707",
    "_type": "_doc",
    "_id": "nFwdf3oBfPMe2V132M4l",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
  • 문서 조회 요청 및 결과
curl --location --request GET 'localhost:9200/nelo2-log-20210707/_doc/nFwdf3oBfPMe2V132M4l?refresh&pretty' \
--header 'Content-Type: application/json'

{
    "_index": "nelo2-log-20210707",
    "_type": "_doc",
    "_id": "nFwdf3oBfPMe2V132M4l",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "body": "xephysis test",
        "projectName": "xep-test",
        "host": "xep-local",
        "logTime": 1625624607
    }
}
  • 문서 검색 요청 및 결과
curl --location --request GET 'localhost:9200/nelo2-log-20210707/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "query_string": {
        "query": "projectName:\"xep-test\"",
        "default_operator": "AND",
        "analyze_wildcard": true,
        "default_field": "body"
    }
  }
}'

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "nelo2-log-20210707",
                "_type": "_doc",
                "_id": "nFwdf3oBfPMe2V132M4l",
                "_score": 0.5753642,
                "_source": {
                    "body": "xephysis test",
                    "projectName": "xep-test",
                    "host": "xep-local",
                    "logTime": 1625624607
                }
            }
        ]
    }
}
  • 문서 검색 요청 및 결과
curl --location --request GET 'localhost:9200/nelo2-log-20210707/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "query_string": {
        "query": "projectName:\"xep-test\" AND host:\"xep-local\" AND body:xep*",
        "default_operator": "AND",
        "analyze_wildcard": true,
        "default_field": "body"
    }
  }
}'

{
    "took": 47,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.1507282,
        "hits": [
            {
                "_index": "nelo2-log-20210707",
                "_type": "_doc",
                "_id": "nFwdf3oBfPMe2V132M4l",
                "_score": 2.1507282,
                "_source": {
                    "body": "xephysis test",
                    "projectName": "xep-test",
                    "host": "xep-local",
                    "logTime": 1625624607
                }
            }
        ]
    }
}
  • 문서를 저장하고 query_string 으로 검색하는 것음 잘 됨 특이사항 없음
    • 퍼콜레이트 쪽에서만 문제가 발생한다?
    • 아니면 퍼콜레이트 쓰려고 만들어 둔 곳에 매핑정보가 없어서?
      • body 말고 다른 필드들도 매핑 정보 등록해보고 검증
  • 퍼콜레이트용 인덱스에 매핑 추가 수정
curl --location --request PUT 'localhost:9200/nelo2-percolate/_mapping?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "properties": {
        "body": {
            "type": "text"
        },
        "projectName": {
            "type": "keyword"
        },
        "host": {
            "type": "keyword"
        },
        "logTime": {
            "type": "date"
        },
        "message": {
            "type": "text"
        },
        "query": {
            "type": "percolator"
        }
    }
}'

중간 요약 2

percolate 에서 사용할 쿼리? 저장 2

  • 요청
curl --location --request PUT 'localhost:9200/nelo2-percolate/_doc/4?refresh&pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "bool": {
            "must": {
                "query_string": {
                    "query": "projectName:\"xep-test\" AND host:\"xep-local\" AND body:xep*",
                    "default_operator": "AND",
                    "analyze_wildcard": true,
                    "default_field": "body"
                }
            }
        }
    }
}'
  • 응답
    • 문서 아이디 4번으로 생성
{
    "_index": "nelo2-percolate",
    "_type": "_doc",
    "_id": "4",
    "_version": 1,
    "result": "created",
    "forced_refresh": true,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}

등록된 문서가 잘 검색되는지 검증 (percolate API 기능 확인)

  • 요청
curl --location --request GET 'localhost:9200/nelo2-percolate/_search?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "body": "xephysis test",
        "projectName":"xep-test",
        "host":"xep-local",
        "logTime": 1625624607
      }
    }
  }
}
'
  • 응답
{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.2615292,
        "hits": [
            {
                "_index": "nelo2-percolate",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.2615292,
                "_source": {
                    "query": {
                        "bool": {
                            "must": {
                                "query_string": {
                                    "query": "projectName:\"xep-test\" AND host:\"xep-local\" AND body:xep*",
                                    "default_operator": "AND",
                                    "analyze_wildcard": true,
                                    "default_field": "body"
                                }
                            }
                        }
                    }
                },
                "fields": {
                    "_percolator_document_slot": [
                        0
                    ]
                }
            }
        ]
    }
}

결과 및 요약

  • 맨위로

'개발관련 > 오픈소스(들?)' 카테고리의 다른 글

Opensearch (aws elasticsearch)  (0) 2021.08.08
kafka 카프카 관리하기  (0) 2021.07.07
로컬 환경에 kakfa 설치 및 구성  (0) 2021.06.23
elasticsearch 의 저장 과정  (0) 2021.05.24
kafka 2.8.0  (0) 2021.05.16
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함