简体   繁体   English

ElasticSearch lucene 子句转换为 ES 语法的查询

[英]ElasticSearch lucene query with subclauses conversion to ES syntax

I've been trying to convert a lucene style query to ES query syntax but I'm getting stuck on sub-clauses.我一直在尝试将 lucene 样式查询转换为 ES 查询语法,但我陷入了子句。 eg例如

(title:history^10 or series:history) and (NOT(language:eng) OR language:eng^5) and (isfree eq 'true' OR (isfree eq 'false' AND owned eq 'abc^5'))

This states that "get me a match for history in 'title' or 'series' but boost the title match AND where the language doesn't have to be english, but if if is then boost it AND where the match is free or where it isn't free then make sure it's owned by customer abc".这表明“让我在'标题'或'系列'中匹配历史但提升标题匹配并且语言不必是英语,但如果是则提升它并且匹配是免费的或在哪里它不是免费的,然后确保它归客户 abc 所有”。

I feel this is a tricky query but it seems to work correctly.我觉得这是一个棘手的查询,但它似乎工作正常。 Converting the clauses to ES syntax is confusing me as I don't really have the concept of brackets.将子句转换为 ES 语法让我感到困惑,因为我真的没有括号的概念。 I think I need to use bool queries... I have the following which I know doesn't apply the criteria correctly - it says you should have (language:eng OR isFree eq 'true' OR owned:abc).我想我需要使用 bool 查询...我知道以下内容没有正确应用标准 - 它说你应该有(语言:eng OR isFree eq 'true' OR owned:abc)。 I can't seem to make the mental leap to build the must/should with NOT's in it.我似乎无法做出精神上的飞跃来构建必须/应该而不是其中的内容。

Help please?请帮助?

  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "history",
            "fields": [
              "title^10.0",
              "series"              
            ]
          }
        }
      ],
      "should": [
        {
          "term": {
            "language": {
              "value": "eng",
              "boost": 5
            }
          }
        },
        {
          "term": {
            "isFree": {
              "value": true
            }
          }
        },
        {
          "term": {
            "owned": {
              "value": "abc",
              "boost": 5
            }
          }
        }
      ]
    }
  },

Your query is almost correct, the only thing that wasn't translated correctly was this part of the query:您的查询几乎是正确的,唯一没有正确翻译的是查询的这一部分:

(isfree eq 'true' OR (isfree eq 'false' AND owned eq 'abc^5'))

If I understand your post correctly, this is basically saying boost the 'owned' field by a factor of five when it's value is 'abc' and the price is free .如果我对你的帖子的理解正确,这基本上是说当它的值为 'abc' 并且价格是免费时,将 'owned' 字段提高五倍 To implement this, you need to use an additional bool query that:要实现这一点,您需要使用一个额外的 bool 查询:

  • Filters results by isFree: trueisFree: true
  • Boosts the owned field of any documents matching abc提升任何匹配abc的文档的拥有字段
"bool": {
  "filter": [
    {
      "term": {
        "isFree": {
          "value": false
        }
      }
    }
  ],
  "must": [
    {
      "term": {
        "owned": {
          "value": "abc",
          "boost": 5
        }
      }
    }
  ]
}

Since this is not intended to limit the result set and only boost results that meet this criteria, the bool query above should be placed inside your parent bool's should section.由于这不是为了限制结果集,而是仅提升满足此条件的结果,因此上面的 bool 查询应放在父 bool 的should部分中。 The final query looks like:最终查询如下所示:

POST /myindex/_search
{
  "explain": true,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "history",
            "fields": [
              "title^10",
              "series"
            ]
          }
        }
      ],
      "should": [
        {
          "term": {
            "language": {
              "value": "eng",
              "boost": 5
            }
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "isFree": {
                    "value": false
                  }
                }
              }
            ],
            "must": [
              {
                "term": {
                  "owned": {
                    "value": "abc",
                    "boost": 5
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Note: Using should and must yield the same results for that inner bool, I honestly am not sure which would be better to use so I just arbitrarily used must .注意:对于内部布尔值,使用shouldmust会产生相同的结果,老实说,我不确定使用哪个更好,所以我只是随意使用must

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM