如何查询elasticserch中的不同字段?

时间:2016-03-20 00:28:04

标签: json elasticsearch

我想查询一个或两个key:value对的elasticsearch,例如:

http://localhost:9200/indexname/_search?pretty=1&q=Date:%222000-12-30T10:11:25%22&q=id:%22unique_id%22

此查询仅考虑unique_id。如果我将Date更改为任何无效值,它仍然会根据Unique_id给我所有值。

知道如何在两个查询中生成AND条件吗?它应该考虑两个查询并相应地提供结果?请指教。谢谢。

3 个答案:

答案 0 :(得分:0)

根据文档,它应该工作

请参阅http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html

也就是说,您可以按如下方式创建查询:

http://localhost:9200/indexname/_search?pretty=1&q=%2BDate%3A2000-12-30T10:11:25+%2Bid%3Aunique_id

注意:%2B被解码为'+',而'+'被解码为''

答案 1 :(得分:0)

试试这个。

GET /index/type/_search
{
  "query": {
    "match": {"Date":"2015-09-17 03:45:00-04"}
  },
  "filter" : {
    "and" : [
                {
                        "match": {"unique_id" : "6324"}
                }
            ]
  }
}

参考链接 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-query.html

答案 2 :(得分:0)

您也可以选择此查询,(简便方法)

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">

var pageNumber = 1;
//var webMethodUrl = '<%=ResolveUrl("~/Mobile.aspx/IncrementTable(pageNumber)") %>'; //alert(webMethodUrl); //this gets a 404-not found error
var webMethodUrl = 'Mobile.aspx/IncrementTable(pageNumber)'; 

$(document).ready(function () {
    $(window).scroll(function () {
        // Get the current vertical position of the scrollbar.  If it's equal to the height of the document minus the 
        // window height then go get some more data
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            // Increment page number and set up the parameters
            pageNumber += 1;
            var params = "{'pageNumber': " + pageNumber + "}";

            // Async post back to the BindDataAsync code behind web method
            $.ajax({
                type: "POST",
                url: webMethodUrl,
                data: params,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (data) {alert('error: ' + data);},
                success: function (data) {alert('success');
                    //if (data != "") {
                        // Take the results from the web service method and append them to the table
                    //    $('#ProductsTable').append(data.d);
                    //}
                }
            });
        }
    });
});

</script>