Nice programing

null이 아니고 비어 있지 않은 ( "")에 대한 Elasticsearch curl 쿼리 생성

nicepro 2020. 12. 3. 19:42
반응형

null이 아니고 비어 있지 않은 ( "")에 대한 Elasticsearch curl 쿼리 생성


null이 아니고 empty ( "")가 아닌 필드 값을 가져 오기 위해 Elasticsearch curl 쿼리를 생성하려면 어떻게해야합니까?

다음은 mysql 쿼리입니다.

select field1 from mytable where field1!=null and field1!="";

null 값과 빈 문자열은 모두 값이 인덱싱되지 않도록합니다.이 경우 exists필터를 사용할 수 있습니다.

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "exists" : {
               "field" : "myfield"
            }
         }
      }
   }
}
'

또는 title필드 에 대한 전체 텍스트 검색과 함께 사용 :

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "filtered" : {
         "filter" : {
            "exists" : {
               "field" : "myfield"
            }
         },
         "query" : {
            "match" : {
               "title" : "search keywords"
            }
         }
      }
   }
}
'

Bool 필터 의 Must-Not 섹션에서 누락 된 필터래핑합니다 . 필드가있는 문서 만 반환하고 "null_value"속성을 true로 설정 한 경우 명시 적으로 null이 아닌 값을 반환합니다.

{
  "query":{
     "filtered":{
        "query":{
           "match_all":{}
        },
        "filter":{
            "bool":{
              "must":{},
              "should":{},
              "must_not":{
                 "missing":{
                    "field":"field1",
                    "existence":true,
                    "null_value":true
                 }
              }
           }
        }
     }
  }
}

@luqmaan이 주석에서 지적했듯이 문서에 따르면 필터 exists 는 null이 아닌 값으로 간주되므로 빈 문자열을 필터링 하지 않습니다 .

따라서 @DrTech의 답변에 추가하여 null 및 빈 문자열 값을 효과적으로 필터링하려면 다음과 같이 사용해야합니다.

{
    "query" : {
        "constant_score" : {
            "filter" : {
                "bool": {
                    "must": {"exists": {"field": "<your_field_name_here>"}},
                    "must_not": {"term": {"<your_field_name_here>": ""}}
                }
            }
        }
    }
}

elasticsearch 5.6에서는 아래 명령을 사용하여 빈 문자열을 필터링해야합니다.

    GET /_search
    {
        "query" : {
            "regexp":{
                "<your_field_name_here>": ".+"
            }
        }
    }  

bool 쿼리와 must 및 must_not의 조합으로 다음과 같이 할 수 있습니다.

GET index/_search
{
    "query": {
        "bool": {
            "must": [
                {"exists": {"field": "field1"}}
            ],
            "must_not": [
                {"term": {"field1": ""}}
            ]
        }
    }
}

Kibana의 Elasticsearch 5.6.5로 이것을 테스트했습니다.


당신은 사용할 수 없습니다 상단에 필터를 누락되었습니다 .

"query": {
  "filtered": {
     "query": {
        "match_all": {}
     },
     "filter": {
        "not": {
           "filter": {
              "missing": {
                 "field": "searchField"
              }
           }
        }
     }
  }
}

5.6.5에서 나를 위해 일한 유일한 해결책은 bigstone1998의 정규식 답변이었습니다. 성능상의 이유로 정규식 검색을 사용하지 않는 것이 좋습니다. 다른 솔루션이 작동하지 않는 이유는 표준 필드가 분석되고 결과적으로 부정 할 빈 문자열 토큰이 없기 때문이라고 생각합니다. 빈 문자열이 널이 아닌 것으로 간주되기 때문에 기존 쿼리는 자체적으로 도움이되지 않습니다.

인덱스를 변경할 수없는 경우 정규식 접근 방식이 유일한 옵션 일 수 있지만 인덱스를 변경할 수있는 경우 키워드 하위 필드를 추가하면 문제가 해결됩니다.

인덱스에 대한 매핑에서 :

"myfield": {
    "type": "text",
    "fields": {
        "keyword": {
            "ignore_above": 256,
            "type": "keyword"
        }
    }
}

그런 다음 간단히 쿼리를 사용할 수 있습니다.

{
  "query": {
    "bool": {
      "must": {
        "exists": {
          "field": "myfield"
        }
      },
      "must_not": {
        "term": {
          "myfield.keyword": ""
        }
      }
    }
  }
}

.keywordmust_not 구성 요소에 유의하십시오 .


우리는 Elasticsearch 버전 1.6을 사용하고 있으며 필드에 대해 null이 아니고 비어 있지 않은 것을 다루기 위해 동료의이 쿼리를 사용했습니다.

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "myfieldName"
              }
            },
            {
              "not": {
                "filter": {
                  "term": {
                    "myfieldName": ""
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

Elastic search Get all record where condition not empty.

const searchQuery = {
      body: {
        query: {
          query_string: {
            default_field: '*.*',
            query: 'feildName: ?*',
          },
        },
      },
      index: 'IndexName'
    };

You can use a bool combination query with must/must_not which gives great performance and returns all records where the field is not null and not empty.

bool must_not is like "NOT AND" which means field!="", bool must exist means its !=null.

so effectively enabling: where field1!=null and field1!=""

GET  IndexName/IndexType/_search
{
    "query": {
      "bool": {
            "must": [{
                "bool": {
                    "must_not": [{
          "term": { "YourFieldName": ""}
                    }
          ]
                    }           }, {
                "bool": {
                    "must": [{
                      "exists" : { "field" : "YourFieldName" }
                    }
                    ]
                }
            }]
      } 
    }
}

ElasticSearch Version: "version": { "number": "5.6.10", "lucene_version": "6.6.1" },

참고URL : https://stackoverflow.com/questions/14745210/create-elasticsearch-curl-query-for-not-null-and-not-empty

반응형