user88831
user88831

Reputation: 317

elasticsearch - search query - ignore order

I'm using a query like

{bool: {must: [{match: {name: "Cat Dog"}}]

This gives me records with name e.g. "Cat Dog Cow" but not e.g. "Cat Cow Dog".

As I read here solutions for it can be used span_near, is this the only way?

I tried query such as :

{"query":{"bool":{"must":[],"must_not":[],"should":[{"span_near":{"slop":12,"in_order":false,"clauses":[{"span_term":{"name":"Cat"}},{"span_term":{"name":"Dog"}}]}}]}}}

But this gives me 0 hits. What can be the issue?

Upvotes: 0

Views: 187

Answers (1)

Bhavya
Bhavya

Reputation: 16172

The match query returns documents that match a provided text, the provided text is analyzed before matching.

Adding a working example

Index mapping:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}

Search Query:

{
  "query": {
    "match": {
      "name": {
        "query": "Cat Dog"
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65230619",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.36464313,
        "_source": {
          "name": "Cat Dog Cow"
        }
      },
      {
        "_index": "65230619",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.36464313,
        "_source": {
          "name": "Cat Cow Dog"
        }
      }
    ]

Search Query using span_near

{
    "query": {
        "span_near" : {
            "clauses" : [
                { "span_term" : { "name" : "cat" } },
                { "span_term" : { "name" : "dog" } }
            ],
            "slop" : 12,
            "in_order" : false
        }
    }
}

Upvotes: 1

Related Questions