Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch match two fields

How can I get this simple SQL query running on elasticsearch ?

SELECT * FROM [mytype] WHERE a = -23.4807339 AND b = -46.60068

I'm really having troubles with it's syntax, multi-match queries doesn't work in my case, which query type should I use?

like image 481
Vinícius Mariano Avatar asked Feb 06 '15 12:02

Vinícius Mariano


People also ask

What is multi match query in Elasticsearch?

The multi_match query provides a convenient shorthand way of running the same query against multiple fields.

What is bool query in Elasticsearch?

Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.

Should minimum should match?

Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

What is Elasticsearch query?

Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses.


2 Answers

For queries like yours bool filter is preferred over and filter. See here the whole story about this suggestion and why is considered to be more efficient.

These being said, I would choose to do it like this:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {"term": {"a": -23.4807339}},
            {"term": {"b": -46.60068}}
          ]
        }
      }
    }
  }
}
like image 95
Andrei Stefan Avatar answered Nov 05 '22 12:11

Andrei Stefan


You can approach this with the and filter. For your example, something like:

{
  "size": 100,
  "query" : {"filtered" : {
    "query" : {"match_all" : {}},
    "filter" : {"and" : [
      "term" : {"a": -23.4807339},
      "term" : {"b": -46.60068}
    ]}
  }}
}

Be sure to direct the query against the correct index and type. Note that I specified the size of the return set as 100 arbitrarily - you'd have to specify a value that fits your use case.

There's more on filtered queries here, and more on the and filter here.

like image 21
rchang Avatar answered Nov 05 '22 12:11

rchang