ElasticSearch: Labelling documents with matching search term

Reizar

I'm using elasticsearch 1.7 and am in need of a way to label documents with what part of a query_string query they match.

I've been experimenting with highlighting, but found that it gets a bit messy with some cases. I'd love to have the document tagged with matching search terms.

Here is the query that I'm using: ( note this is a ruby hash that later gets encoded to JSON )

{
  query: {
    query_string: {
      fields: ["title^10", "keywords^4", "content"],
      query: query_string,
      use_dis_max: false
    }
  },
  size: 20,
  from: 0,
  sort: [
    { pub_date: { order: :desc }},
    { _score:   { order: :desc }}
  ]
}

The query_string variable is based off user followed topics and might look something like this: "(the AND walking AND dead) OR (iphone) OR (video AND games)"

Is there any option I can use so that documents returned would have a property matching a search term like the walking dead or (the AND walking AND dead)

Val

If you're ready to switch to using bool/should queries, you can split the match on each field and use named queries, then in the results you'll get the name of the query that matched.

It goes basically like this: in a bool/should query, you add one query_string query per field and name the query so as to identify that field (e.g. title_query for the title field, etc)

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "fields": [
              "title^10"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "title_query"
          }
        },
        {
          "query_string": {
            "fields": [
              "keywords^4"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "keywords_query"
          }
        },
        {
          "query_string": {
            "fields": [
              "content"
            ],
            "query": "query_string",
            "use_dis_max": false,
            "_name": "content_query"
          }
        }
      ]
    }
  }
}

In the results, you'll then get below the _source another array called matched_queries which contains the name of the query that matched the returned document.

"_source": {
    ...
},
"matched_queries": [
    "title_query"
],

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

term frequency of documents with Nest Elasticsearch

From Dev

Elasticsearch: Matching documents with an array in it

From Dev

Prefer matching search term in beginning of search result rather than in the end using elasticsearch

From Dev

Prefer matching search term in beginning of search result rather than in the end using elasticsearch

From Dev

Term not found in Search but present in a term vector in Elasticsearch

From Dev

Finding the number of documents that contain a term in elasticsearch

From Dev

Matching across Multiple documents with ElasticSearch

From Dev

MYSQL Search for matching letters within term

From Dev

Elasticsearch: Term search not working on special characters

From Dev

Elasticsearch bool search matching incorrectly

From Dev

Filter OUT matching documents in elasticsearch with aggregation

From Dev

Filter OUT matching documents in elasticsearch with aggregation

From Dev

Term, nested documents and must_not query incompatible in ElasticSearch?

From Dev

Term, nested documents and must_not query incompatible in ElasticSearch?

From Dev

Remove duplicate documents from a search in Elasticsearch

From Dev

Elasticsearch - boosting specific documents in every search

From Dev

Rails 4 returning records from multiple tables matching a search term

From Dev

Elasticsearch starts with search by using Partial Matching queries

From Dev

elasticsearch case insensitive term filter search on not_analyzed field

From Dev

ElasticSearch how display all documents matching date range aggregation

From Dev

Elasticsearch: Search Performance of index with large documents (PDF,doc,txt) is slow

From Dev

how to search child documents with the same parent id in Elasticsearch?

From Dev

how to search documents by field property in Elasticsearch using java?

From Dev

Elasticsearch, term suggester returns

From Dev

ElasticSearch term aggregation

From Dev

Scoring by term position in ElasticSearch?

From Dev

Elasticsearch multi term filter

From Dev

Elasticsearch term filter not working?

From Dev

elasticsearch: term query fails

Related Related

  1. 1

    term frequency of documents with Nest Elasticsearch

  2. 2

    Elasticsearch: Matching documents with an array in it

  3. 3

    Prefer matching search term in beginning of search result rather than in the end using elasticsearch

  4. 4

    Prefer matching search term in beginning of search result rather than in the end using elasticsearch

  5. 5

    Term not found in Search but present in a term vector in Elasticsearch

  6. 6

    Finding the number of documents that contain a term in elasticsearch

  7. 7

    Matching across Multiple documents with ElasticSearch

  8. 8

    MYSQL Search for matching letters within term

  9. 9

    Elasticsearch: Term search not working on special characters

  10. 10

    Elasticsearch bool search matching incorrectly

  11. 11

    Filter OUT matching documents in elasticsearch with aggregation

  12. 12

    Filter OUT matching documents in elasticsearch with aggregation

  13. 13

    Term, nested documents and must_not query incompatible in ElasticSearch?

  14. 14

    Term, nested documents and must_not query incompatible in ElasticSearch?

  15. 15

    Remove duplicate documents from a search in Elasticsearch

  16. 16

    Elasticsearch - boosting specific documents in every search

  17. 17

    Rails 4 returning records from multiple tables matching a search term

  18. 18

    Elasticsearch starts with search by using Partial Matching queries

  19. 19

    elasticsearch case insensitive term filter search on not_analyzed field

  20. 20

    ElasticSearch how display all documents matching date range aggregation

  21. 21

    Elasticsearch: Search Performance of index with large documents (PDF,doc,txt) is slow

  22. 22

    how to search child documents with the same parent id in Elasticsearch?

  23. 23

    how to search documents by field property in Elasticsearch using java?

  24. 24

    Elasticsearch, term suggester returns

  25. 25

    ElasticSearch term aggregation

  26. 26

    Scoring by term position in ElasticSearch?

  27. 27

    Elasticsearch multi term filter

  28. 28

    Elasticsearch term filter not working?

  29. 29

    elasticsearch: term query fails

HotTag

Archive