复杂的弹性搜索查询休息

时间:2017-11-08 10:41:13

标签: rest elasticsearch lucene

使用经典asp中的Rest我可以在执行简单搜索时从索引中检索数据,例如:

Set xml = Server.CreateObject("Microsoft.XMLHTTP")

xml.Open "GET", "http://elastic:changeme@10.128.128.109:9200/myindex/_search?q='fred blogs',_source='path.real'", False

我现在想要从一个更复杂的查询中检索数据,如果我使用Kibana会看起来像这样:

GET newsindex/_search
{
"query": {
"function_score": {
  "query": { "bool" : {"should": [ 
   { "term": { "headline": "brown" } },
   { "term": {  "bodytext": "brown" } },
   { "term": { "headline": "fox" } },
   { "term": { "bodytext": "fox" } }
]}},
  "functions": [
    {
      "filter": {"match" : { "headline": "brown fox" }},
      "weight": 2
    },
    {
      "filter": {"match" : { "bodytext": "brown fox" }},
      "weight": 1
    },
    {
      "filter": {"match" : { "bodytext": "fox" }},
      "weight": 3
    },
    {
      "filter": {"match" : { "bodytext": "brown" }},
      "weight": 3
    },
    {
      "filter": {"match" : { "headline": "brown" }},
      "weight": 4
    },
    {
      "filter": {"match" : { "headline": "fox" }},
      "weight": 4
    }
    ],
  "score_mode": "sum"
}
},"sort": [
{
  "_score": {
    "order": "asc"
  }
}
], 
"_source":[ "headline", "bodytext" ]
}

如何使用Rest传递此查询?我将动态建立这样的查询以通过休息。但我需要让构造正确。

1 个答案:

答案 0 :(得分:0)

通过使用curl命令,您可以执行以下操作:

curl -XGET "http://elastic:changeme@10.128.128.109:9200/myindex/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "headline": "brown"
              }
            },
            {
              "term": {
                "bodytext": "brown"
              }
            },
            {
              "term": {
                "headline": "fox"
              }
            },
            {
              "term": {
                "bodytext": "fox"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "headline": "brown fox"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "match": {
              "bodytext": "brown fox"
            }
          },
          "weight": 1
        },
        {
          "filter": {
            "match": {
              "bodytext": "fox"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "match": {
              "bodytext": "brown"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "match": {
              "headline": "brown"
            }
          },
          "weight": 4
        },
        {
          "filter": {
            "match": {
              "headline": "fox"
            }
          },
          "weight": 4
        }
      ],
      "score_mode": "sum"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ],
  "_source": [
    "headline",
    "bodytext"
  ]
}'

您可以使用curl在数据中发送查询。我确信你的Rest客户端也可以实现同样的目的。只需检查如何使用您的Rest客户端发送数据。

post可能有所帮助。将示例中的DataToSend变量替换为您的查询,看看是否有效。

相关问题