【问题标题】:Trouble filtering results from elasticsearch using Ajax使用 Ajax 从 elasticsearch 过滤结果时遇到问题
【发布时间】:2013-01-02 16:02:59
【问题描述】:

我正在使用 Amplify 对 elasticsearch 进行 Ajax 调用,但是在过滤结果时遇到问题。由于我将所有内容都作为 URL 传递,因此我不确定如何格式化它。 当我通过 firstName:John 作为 searchCriteria 时,以下返回 4 个结果。

self.url = "http://leServer:lePort/people/person/_search?q=" + self.searchCriteria;

如果我有 firstName:John&lastName:Smith,它会返回 6 个结果,因为有 2 个 smith 记录的名字不是 John。

如果我从命令提示符运行以下命令,我会得到一个预期的结果。

curl -XGET "http://leServer:lePort/people/person/_search?pretty=true" -d "{
  \"query\": {
    \"filtered\": {
      \"query\": {
        \"text\": {
          \"firstName\": \"John\"
        }
      },
      \"filter\": {
        \"query\": {
          \"text\": {
            \"lastName\": \"Smith\"
          }
        }
      }
    }
  }
}"

我尝试使用以下作为我的 Ajax 调用,但它没有返回任何内容。我还尝试了 curl 请求所具有的\"

self.url = "http://leServer:lePort/people/person/_search?" +"-d"+"{query:{filtered:{query:{text:{firstName:John}},filter:{query:{text:{lastName:Smith}}}}}}"

【问题讨论】:

    标签: ajax elasticsearch amplifyjs


    【解决方案1】:

    查询应该是 url 编码的+firstName:John +lastName:Smith。顺便说一句,将您的弹性搜索服务器暴露给外界可能是个坏主意。

    【讨论】:

    • 我尝试使用 _search?q=lastName:Smith+firstName:John 并返回 6 个结果,其中 4 个带有名字 John,2 个带有姓氏 Smith。还有关于对外曝光,它是一个内网站点。
    • 查询的默认运算符是 OR,您可以通过在 URL 中添加 default_operator=AND 或在每个字段前添加 + 来更改它。试试这个:?q=%2blastName:Smith+%2bfirstName:John
    • 效果很好。但是我很好奇为什么我需要同时使用 + 和 %2b,而 ++ 并没有给出想要的结果。
    猜你喜欢
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多