首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Elasticsearch must_not不适用于多个字段。

Elasticsearch must_not不适用于多个字段。
EN

Stack Overflow用户
提问于 2016-02-08 11:45:54
回答 2查看 1.7K关注 0票数 0

我使用多字段fields.html

下面是我映射的一部分:

代码语言:javascript
运行
复制
...
"Diagnosis": {
               "type": "string",
               "fields":{
                 "not_analyzed":{
                    "type":"string",
                    "index":"not_analyzed"
                 }
                }
            }
...

我运行以下查询:

代码语言:javascript
运行
复制
curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search?pretty' -d '{"query": {"bool": {"must_not": [{"match": {"Diagnosis.not_analyzed": "F06.4 - Organic anxiety disorder"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

尽管有must_not子句,上述查询在其他结果中返回"F06.4 -有机焦虑症“字符串。

我可以用“焦虑”这个词来排除所有的结果

代码语言:javascript
运行
复制
curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search?pretty' -d '{"query": {"bool": {"must_not": [{"match": {"Diagnosis": "anxiety"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

但目标是从结果中只排除确切的字符串"F06.4 -有机焦虑症“。我怎么能这么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-08 12:52:26

尝试这样做:( 1)将映射更改为小写,但不要用word (默认映射)来剪切它们。

代码语言:javascript
运行
复制
curl -XPUT 'localhost:9200/...../' -d '{
 "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "keylower":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
     }
  },
  "mappings":{
        "specimens" : {
            "_all" : {"enabled" : true},
            "_index" : {"enabled" : true},
            "_id" : {"index": "not_analyzed", "store" : false},
            "properties" : {

                "Diagnosis" : {"type" : "string",   "store" : "yes","index": "not_analyzed" } 

            }
        }
    }
}

2)这将只返回不包含“有机焦虑症”(其中*可以是任何单词)的数据。

代码语言:javascript
运行
复制
{
    "query" : {
        "bool" : {
            "must_not" : [{
                    "wildcard" : {
                        "Diagnosis" : {
                            "value" : "*organic anxiety disorder*"
                        }
                    }

                }
            ]
        }
    }
}   

3)使用严格搜索排除数据:

代码语言:javascript
运行
复制
{
    "query" : {
        "bool" : {
            "must_not" : [{
                    "term" : {
                        "Diagnosis.not_analyzed" : "f06.4 - organic anxiety disorder"
                    }
                }
            ]
        }
    }
} 
票数 1
EN

Stack Overflow用户

发布于 2016-02-08 12:21:17

如果要排除确切的字符串,请使用term query like。

代码语言:javascript
运行
复制
curl -XGET 'elasticsearch_server:9200/db-index/Diagnosis/_search? pretty' -d '{"query": {"bool": {"must_not": [{"term": {"Diagnosis.not_analyzed": "F06.4 - Organic anxiety disorder"}}], "must": [{"match": {"Diagnosis": "Dementia disease disorder"}}]}}}'

希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35268891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档