Elasticsearch OR查询

时间:2015-05-07 11:37:19

标签: elasticsearch elastica

有谁能告诉我如何在弹性支持中编写下面的Mysql查询

DateTime start = Convert.ToDateTime(txtStart.Text);
DateTime end = Convert.ToDateTime(txtEnd.Text);
TimeSpan datedifference = end.Subtract(start);
int dateCount = datedifference.Days + 1;

我尝试了多种互联网解决方案

Select * from `table` WHERE `Name`='A' OR `Name`='B' order by `rank` DESC

并尝试了下面的代码

{

"sort":{"rank":{"order":"desc"}}, 

"query": {

    "query_string" : {
        "fields" : ["Name"],
        "query" : "A OR B"
    }


}

3 个答案:

答案 0 :(得分:0)

您可以使用Bool-Filter和Order on all Documents:

{
  "query": {
    "match_all": {}
  },
  "filter": {
    "bool": {
      "should": [
        {
          "term": {
            "Name": "A"
          }
        },{
          "term": {
            "Name": "B"
          }
        }
      ]
    }
  },"sort": [
    {
      "rank": {
        "order": "desc"
      }
    }
  ]
}

或者有一个带有范围查询的子集:

"query": {
    "range": {
      "JoinDate": {
        "lte": 1431051540
      }
    }
  }

答案 1 :(得分:0)

这是我目前的映射

{
    "class": {
        "mappings": {
            "students": {
                "properties": {
                    "Name": {
                        "type": "string"
                    },
                    "rank": {
                        "type": "string"
                    },
                    "Description": {
                        "type": "string"
                    },
                    "Image": {
                        "type": "string"
                    },
                    "JoinDate": {
                        "type": "date",
                        "format": "dateOptionalTime"
                    }
                }
            }
        }
    }
}

答案 2 :(得分:0)

试试Terms filter。下面等同于您编写的SQL查询。

curl -XGET 'http://localhost:9200/_search?pretty' -d '{
    "filter": {
        "terms": {
            "Name": ["A", "B"]
        }
    },
    "sort": {
        "rank": {
            "order": "desc"
        }
    }
}'