0
votes

I wanna perform a full-text search, but I also wanna use one or many possible filters. The simplified structure of my document, when searching with /things/_search?q=*foo*:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "things",
                "_type": "thing",
                "_id": "63",
                "_score": 1,
                "fields": {
                    "name": [
                        "foo bar"
                    ],
                    "description": [
                        "this is my description"
                    ],
                    "type": [
                        "inanimate"
                    ]
                }
            }
        ]
    }
}

This works well enough, but how do I combine filters with a query? Let's say I wanna search for "foo" in an index with multiple documents, but I only want to get those with type == "inanimate"?

This is my attempt so far:

{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "query": "*foo*"
                }
            },
            "filter": {
                "bool": {
                    "must": {
                        "term": { "type": "inanimate" }
                    }
                }
            }
        }
    }
}

When I remove the filter part, it returns an accurate set of document hits. But with this filter-definition it does not return anything, even though I can manually verify that there are documents with type == "inanimate".

1
Are you mixing up type and _type??IanGabes
How do you index your data?paweloque
I'm not sure what you mean, but I use logstash-input-jdbc to index data from a MySQL database?Johan Norberg
could you post the output of curl -XGET 'http://localhost:9200/your_index/_mapping/your_typeChintanShah25
Well obviously this example is mocked to not reveal real data. But the field "type" is of type (heh) "string". I haven't done any "explicit mapping".Johan Norberg

1 Answers

2
votes

Since you have not done explicit mapping, term query is looking for an exact match. you need to add "index : not_analyzed" to type field and then your query will work.

This will give you correct documents

{
  "query": {
    "match": {
      "type": "inanimate"
    }
  }
}

but this is not the solution, You need do explicit mapping as I said.