elasticsearch - ElasticSearch应该查询

标签 elasticsearch

我想创建与该SQL查询相同的ElasticSearch查询

select *
from main.adverts
where user_id = 4
      and 
      (title ilike '%продать / купить%'
        or description ilike '%продать / купить%'
      )

我的尝试是:
{
  "query" :{
  "bool" : {
    "must" : [
      {
        "term" : {
          "user.id" : {
            "value" : 4,
            "boost" : 1.0
          }
        }
      }
    ],
    "should" : [
      {
        "bool" : {
          "must" : [
            {
              "match" : {
                "title" : {
                  "query" : "продать",
                  "operator" : "OR",
                  "fuzzy_transpositions" : false,
                  "boost" : 1.0
                }
              }
            },
            {
              "wildcard" : {
                "title" : {
                  "wildcard" : "купить*",
                  "boost" : 1.0
                }
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "match" : {
                "description" : {
                  "query" : "продать",
                  "operator" : "OR",
                  "fuzzy_transpositions" : false,
                  "boost" : 1.0
                }
              }
            },
            {
              "wildcard" : {
                "description" : {
                  "wildcard" : "купить*",
                  "boost" : 1.0
                }
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

但是它不能正常工作。作为此查询的结果,我得到了不包含短语“продать/купить”的记录。

我认为问题出在ElasticSearch查询的“应该”部分,但无法获得具体位置。

你能指出我的错误在哪里吗?

最佳答案

看来我已经找到解决方法

{
  "from" : 0, "size" : 60,
  "_source" : ["title", "description"],
  "query" :{
  "bool" : {
    "must" : [
      {
        "term" : {
          "user.id" : {
            "value" : 4,
            "boost" : 1.0
          }
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "match" : {
                "title" : {
                  "query" : "продать",
                  "operator" : "OR",
                  "prefix_length" : 0,
                  "max_expansions" : 50,
                  "fuzzy_transpositions" : false,
                  "lenient" : false,
                  "zero_terms_query" : "NONE",
                  "boost" : 1.0
                }
              }
            },
            {
              "wildcard" : {
                "title" : {
                  "wildcard" : "купить*",
                  "boost" : 1.0
                }
              }
            }
          ],
          "should" : [
            {
              "bool" : {
                "must" : [
                  {
                    "match" : {
                      "description" : {
                        "query" : "продать",
                        "operator" : "OR",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "fuzzy_transpositions" : false,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                  },
                  {
                    "wildcard" : {
                      "description" : {
                        "wildcard" : "купить*",
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

关于elasticsearch - ElasticSearch应该查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52814931/

相关文章:

ElasticSearch 安全和端口

Elasticsearch 访问日志

mongodb - 查询数据以在AWS中实现最小延迟的最佳方法

elasticsearch - 使用NEST V5.4重新索引-ElasticSearch

java - 将 PreBuiltTransportClient 与 elasticsearch 5 结合使用

spring-boot - Elasticsearch在启动期间引发错误

elasticsearch - 如何在ElasticSearch中结合fuzzy_like_this和过滤器

amazon-web-services - Elasticsearch-按多个数组中数组匹配的分数排序

elasticsearch - 使用GCP/GAE搜索和缓存API

java - 在 Reactor 中实现 while 循环以获取最新的 Elasticsearch 索引