1

I'm not very strong in Elasticsearch. I'm trying to set up search in my app and got some strange problems. I have two documents:

{
  "title": "Second insight"
  "content": "Bla bla bla"
  "library": "workspace"
}
{
  "title": "Test source"
  "content": "Bla bla bla"
  "library": "workspace"
}

Then, I want to be able to make a search by text fields like title and content and apply some filters on fields like library. I have a query:

{
    "query": {
      "bool": { 
        "should": [
          { "match": { "title": "insight" }}
        ],
        "filter": [
          {
            "term": {
              "library": "workspace"
            }
          }
        ]
      }
    }
}

Despite the fact that I clearly defined title to be matched to insight, the query above returns both documents, not only the first one.

If I remove filter block:

{
    "query": {
      "bool": { 
        "should": [
          { "match": { "title": "insight" }}
        ]
      }
    }
}

the query returns correct results.

Then, I also tried to make a partial search. For some reasons, the query uses ins instead of insight below doesn't work, so, it returns empty list:

{
    "query": {
      "bool": { 
        "should": [
          { "match": { "title": "ins" }}
        ]
      }
    }
}

How should I make partial search? And how can I set up filters correctly? In other words, how to make a search partial query by some fields, but at the same time filtered by other fields?

Thanks.

1 Answer 1

3

You need to supply minimum_should_match in your first query.

I did the following and only got a single document (your desired outcome)

POST test_things/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1, 
      "should": [
        {
          "match": {
            "title": "insight"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "library": "workspace"
          }
        }
      ]
    }
  }
}

As for why ins doesn't work, it depends on your mapping + analyzer being used. You are matching against analyzed terms in the index, if you want to match against ins you need to change your analyzer (possibly using the ngram tokenizer) or use a wildcard query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.