【问题标题】:Elastic Search "must" in array弹性搜索“必须”在数组中
【发布时间】:2017-11-21 00:13:48
【问题描述】:

我是 Elastic Search 的新手。

我已经在 elasticsearch 中索引了下一个对象:

                "doc": [
                    {
                        "partes": [
                            {
                                "algo": [
                                    {
                                        "Category": "Therapeutic or Preventive Procedure",
                                        "Neg": "false",
                                        "CandidatePreferred": "Obstetric Surgical Procedures",
                                        "CandidateScore": "1000",
                                        "CandidateMatched": "Obstetric Surgical",
                                        "Phrase": "OBSTETRIC SURGICAL",
                                        "CUI": "C0038906"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "partes": [
                            {
                                "algo": [
                                    {
                                        "Category": "Intellectual Product",
                                        "Neg": "false",
                                        "CandidatePreferred": "Given name",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "given",
                                        "Phrase": "given of discharge",
                                        "CUI": "C3244317"
                                    },
                                    {
                                        "Category": "Body Substance",
                                        "Neg": "false",
                                        "CandidatePreferred": "Discharge, body substance",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "Discharge",
                                        "Phrase": "given of discharge",
                                        "CUI": "C2926602"
                                    }
                                ]
                            },
                            {
                                "algo": [
                                    {
                                        "Category": "Health Care Activity",
                                        "Neg": "false",
                                        "CandidatePreferred": "Patient Discharge",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "Discharge",
                                        "Phrase": "given of discharge",
                                        "CUI": "C0030685"
                                    },
                                    {
                                        "Category": "Intellectual Product",
                                        "Neg": "false",
                                        "CandidatePreferred": "Given name",
                                        "CandidateScore": "790",
                                        "CandidateMatched": "given",
                                        "Phrase": "given of discharge",
                                        "CUI": "C3244317"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }

我的目标是获取在同一元素 algo 中有两个 CUI 的元素,即 doc 中有一个 algo 谁同时拥有 CUI:C3244317C2926602

我正在尝试进行下一次搜索:

{
    "query": {        
        "nested": {
            "path": "doc",
            "query": {
                "nested":{
                    "path":"doc.partes",
                    "query": {
                        "nested": {
                            "path":"doc.partes.algo",
                            "query": {
                                "bool": {
                                    "must": [
                                        { "term": { "doc.partes.algo.CUI": "C3244317" }},
                                        { "term": { "doc.partes.algo.CUI": "C2926602" }}
                                    ]

                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

但我没有任何结果:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

我得到了 should 而不是 must 的结果,但这不是我一直在寻找的行为。

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0


    【解决方案1】:

    默认的standard analyzer 会删除大多数标点符号,将文本分解为单个单词,并将它们小写。

    所以可能 C3244317 被索引为 c3244317,即使用小写的“c”。

    您不需要这么大的查询。这应该可以正常工作:

    {  
       "query":{  
          "bool":{  
             "must":[  
                {  
                   "term":{  
                      "doc.partes.algo.CUI":"c3244317"
                   }
                },
                {  
                   "term":{  
                      "doc.partes.algo.CUI":"c2926602"
                   }
                }
             ]
          }
       }
    }
    

    【讨论】:

    • 成功了!最奇怪的是,“应该”是大写的。
    • 标准分析器应该在查询和索引时间都执行,所以它不应该对差异负责。必须意味着两个子句都应该是正确的(逻辑 AND),并且应该只有一个子句应该匹配(逻辑 OR)。您可以使用 elasticsearch explain api 来查看发生了什么。
    猜你喜欢
    • 2019-06-20
    • 2017-09-10
    • 2021-02-04
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2017-03-17
    相关资源
    最近更新 更多