【问题标题】:SQL like query in ELasticsearch with AND OR在 Elasticsearch 中使用 AND OR 进行类似 SQL 的查询
【发布时间】:2018-08-04 04:20:35
【问题描述】:

您好,我还在玩 elasticsearch(V6.1.3),发现它很神奇,但仍在挣扎。我可以轻松地进行单项查询搜索,并且我的设置代码如下:

from elasticsearch import Elasticsearch
es = Elasticsearch()
if not es.indices.exists(index="my-index"):
    customset={
        "settings": {
            "analysis": {
                "analyzer": {
                    "ngram_analyzer": {
                        "tokenizer": "ngram_tokenizer",
                        "filter": [
                            "lowercase",
                            "asciifolding"
                        ]
                    }
                },
                "tokenizer": {
                    "ngram_tokenizer": {
                        "type": "ngram",
                        "min_gram": 3,
                        "max_gram": 100,
                        "token_chars": [
                            "letter",
                            "digit"
                        ]
                    }
                }
            }
        },
        "mappings": {
            "my-type": {
                "properties": {
                    "demodata": {
                        "type": "text",
                        "fields": {
                            "ngram": {
                                "type": "text",
                                "analyzer": "ngram_analyzer",
                                "search_analyzer": "standard"
                            }
                        }
                    },
                    "Field1": {
                        "type": "text",
                        "fields": {
                            "ngram": {
                                "type": "text",
                                "analyzer": "ngram_analyzer",
                                "search_analyzer": "standard"
                            }
                        }
                    }
                }
            }
        }
    }

    es.indices.create(index="my-index", body=customset, ignore=400)
    docs=[
    { "demodata": "hELLO" ,"field1":"Alex","field2":"Red"},
    { "demodata": "hi" ,"field1":"Tom","field2":"Blue"},
    { "demodata": "bye" ,"field1":"Jack","field2":"Green"},
    { "demodata": "HeLlo WoRld!","field1":"Robert","field2":"Yellow" },
    { "demodata": "xyz@abc.com","field1":"Dave","field2":"White" }
    ]
    for doc in docs:
    res = es.index(index="my-index", doc_type="my-type", body=doc)



es.indices.refresh(index="my-index")
res=helpers.scan(client=es,scroll='2m',index="my-index", doc_type="my-type",query={"query": {"match": {"demodata.ngram": "Hello"}}})

现在我想做以下类似 SQL 的查询(假设我的 SQL 表名:my-type):

SELECT * FROM my-type WHERE (demodata LIKE '%ell%' OR demodata LIKE '%orld%') AND (field1 LIKE '%red%' OR demodata LIKE '%yellow%')

这意味着我必须在不同领域使用多个术语进行搜索。谁能建议我怎么做? 谢谢

【问题讨论】:

  • 您正在设置Field1,然后使用field1...我相信elasticsearch会将所有内容都小写,但我不确定。最好坚持使用小写的键名

标签: python elasticsearch search n-gram elasticsearch-analyzers


【解决方案1】:
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1, 
            "should": [
              {
                "wildcard": {
                  "demodata": {
                    "value": "*ell*"
                  }
                }
              },
              {
                "wildcard": {
                  "demodata": {
                    "value": "*orld*"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1, 
            "should": [
              {
                "wildcard": {
                  "demodata": {
                    "value": "*yellow*"
                  }
                }
              },
              {
                "wildcard": {
                  "field1": {
                    "value": "*red*"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

以上是一种方法(我使用的是 ES 5.3.3)。 这不会为您提供的数据集产生任何结果。 试着玩一下。还有其他方法可以实现相同的行为,但这是最直接的一种

【讨论】:

  • 条款查询呢?
  • terms 查询是为了严格相等
【解决方案2】:

我使用的是 ES 5.6.2

{
"query": {
    "bool": {
        "should": {             
            "multi_match": {
                "query": "orld ell",
                "fields": [
                    "demodata"                                         
                ]
            }
        },
        "should": [
            "multi_match": {
                "query": "red",
                "fields": [
                    "field1"                       
                ]
            },
            "multi_match": {
                "query": "yellow",
                "fields": [
                    "demodata"                       
                ]
            }
        ]
    }
}

}

【讨论】:

  • 这是答案还是评论?请更具描述性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 2020-07-11
  • 2021-12-23
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多